Java a code give out what is on the image. Essentially write in the main function so that it prints what is in the image. Im stuck at that part it would be much appreciated if you could help. package javaapplication31; import java.util.Random; import java.util.Scanner; /** * * @author */ public class JavaApplication31 { String name; /** * @param args the command line arguments */ public static void main(String[] args) { int totalScore = 300; int itrcount =12; int reward; char direction; int x = 0; int y = 0; System.out.println(reward()); System.out.println(inputDirection()); } public static void displayInfo(int x, int y, int itrCount, int totalScore){ x = 0; y = 0; itrCount = 0; totalScore =300; System.out.println(""); } public static boolean doesExceed (int x,int y, char direction){ return (y > 4 && Character.toLowerCase(direction) == 'u' || x < 0 && Character.toLowerCase(direction) == '1'|| y < 0 && Character.toLowerCase(direction) == 'd' || x > 4 && Character.toLowerCase(direction) == 'r'); } public static int reward(){ Random rand = new Random (); int dice = rand.nextInt(6)+1; switch (dice){ case 1: System.out.println("Dice: 1, reward: -100"); return -100; case 2: System.out.println("Dice: 2, reward: -200"); return -200; case 3: System.out.println("Dice: 3, reward: -300"); return -300; case 4: System.out.println("Dice: 4, reward: 300"); return 300; case 5: System.out.println("Dice: 5, reward: 400"); return 400; default: System.out.println("Dice: 6, reward: 600"); return 600; } } public static int punishOrMercy(char direction, int reward){ if (reward < 0 && direction == 'u'){ Random rand = new Random(); int coin = rand.nextInt(3); if (coin ==0) System.out.println("Coin: tail | Mercy, the negative reward is removed. "); else System.out.println("Coin: head | No mercy, the negative reward is applied"); } return reward; } public static String toTitleCase(String str){ char d = str.charAt(0); String str1 = str.substring(1, str.indexOf (' ') +2); String str2 = str.substring(str.indexOf(' '), str.indexOf(' ') +2); char d1 = str2.charAt(1); String str3 = str.substring(str.indexOf(' ') +2, str.length()); String str4 = Character.toTitleCase(d1) + str3.toLowerCase(); return str4; } public static void evaluation(int totalScore){ Scanner console = new Scanner(System.in); System.out.println("Please enter your name (only two words) "); String name = console.nextLine(); if (totalScore>2000) System.out.println("Victory! " + toTitleCase(name) + ",your score is " +totalScore); else System.out.println("Mission failed! " + toTitleCase(name) + ", your score is " +totalScore); } public static char inputDirection(){ Scanner console = new Scanner(System.in); char directionLetter = ' '; boolean isValid; do{ System.out.println("Please input a valid direction "); directionLetter = console.next().toLowerCase().charAt(0); isValid = (directionLetter == 'u' || directionLetter == 'd' || directionLetter == 'l' || directionLetter == 'r'); } while (!isValid); return directionLetter; } public static boolean isGameOver(int x, int y, int totalScore, int itrCount){ if (itrCount > 20 || totalScore < 1000 || totalScore > 1000 || x ==4 && y ==4 || x==0 && y==0){ evaluation(totalScore); return false; } else return false; }
Write in Java a code give out what is on the image.
Essentially write in the main function so that it prints what is in the image.
Im stuck at that part it would be much appreciated if you could help.
package javaapplication31;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author
*/
public class JavaApplication31 {
String name;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int totalScore = 300;
int itrcount =12;
int reward;
char direction;
int x = 0;
int y = 0;
System.out.println(reward());
System.out.println(inputDirection());
}
public static void displayInfo(int x, int y, int itrCount, int totalScore){
x = 0;
y = 0;
itrCount = 0;
totalScore =300;
System.out.println("");
}
public static boolean doesExceed (int x,int y, char direction){
return (y > 4 && Character.toLowerCase(direction) == 'u' || x < 0 && Character.toLowerCase(direction) == '1'||
y < 0 && Character.toLowerCase(direction) == 'd' || x > 4 && Character.toLowerCase(direction) == 'r');
}
public static int reward(){
Random rand = new Random ();
int dice = rand.nextInt(6)+1;
switch (dice){
case 1:
System.out.println("Dice: 1, reward: -100");
return -100;
case 2:
System.out.println("Dice: 2, reward: -200");
return -200;
case 3:
System.out.println("Dice: 3, reward: -300");
return -300;
case 4:
System.out.println("Dice: 4, reward: 300");
return 300;
case 5:
System.out.println("Dice: 5, reward: 400");
return 400;
default:
System.out.println("Dice: 6, reward: 600");
return 600;
}
}
public static int punishOrMercy(char direction, int reward){
if (reward < 0 && direction == 'u'){
Random rand = new Random();
int coin = rand.nextInt(3);
if (coin ==0)
System.out.println("Coin: tail | Mercy, the negative reward is removed. ");
else
System.out.println("Coin: head | No mercy, the negative reward is applied");
}
return reward;
}
public static String toTitleCase(String str){
char d = str.charAt(0);
String str1 = str.substring(1, str.indexOf (' ') +2);
String str2 = str.substring(str.indexOf(' '), str.indexOf(' ') +2);
char d1 = str2.charAt(1);
String str3 = str.substring(str.indexOf(' ') +2, str.length());
String str4 = Character.toTitleCase(d1) + str3.toLowerCase();
return str4;
}
public static void evaluation(int totalScore){
Scanner console = new Scanner(System.in);
System.out.println("Please enter your name (only two words) ");
String name = console.nextLine();
if (totalScore>2000)
System.out.println("Victory! " + toTitleCase(name) + ",your score is " +totalScore);
else
System.out.println("Mission failed! " + toTitleCase(name) + ", your score is " +totalScore);
}
public static char inputDirection(){
Scanner console = new Scanner(System.in);
char directionLetter = ' ';
boolean isValid;
do{
System.out.println("Please input a valid direction ");
directionLetter = console.next().toLowerCase().charAt(0);
isValid = (directionLetter == 'u' || directionLetter == 'd' || directionLetter == 'l' ||
directionLetter == 'r');
} while (!isValid);
return directionLetter;
}
public static boolean isGameOver(int x, int y, int totalScore, int itrCount){
if (itrCount > 20 || totalScore < 1000 || totalScore > 1000 || x ==4 && y ==4 || x==0 && y==0){
evaluation(totalScore);
return false;
}
else return false;
}
}
Step by step
Solved in 3 steps