My main question is how do I do the following with declaring onlyone int value r
ID: 3612837 • Letter: M
Question
My main question is how do I do the following with declaring onlyone int value rather than the 2 used currently (state & res).Also, when I use "Scanner stdIn = new Scanner(System.in);" I can'tget it to compile, but my book says it should using this ratherthan "Scanner in" - how do I do this? Finally, since adding theinteger value this way is sort of inherent in the code, you can'treally say "System.out.println("Enter an integer: "), can you? Iwant to say that and then have a line of white space, such as:Enter an integer: 3
You have chosen...
etc.
----------------------
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int state = 1,res;
boolean coin, purse = true, horse=true;
Scanner in=new Scanner(System.in);
while (true) {
if (state == 1){
System.out.println(" You find money and meet an oldman. What do you do? ");
System.out.println("1: accept the money");
System.out.println("2: decline the money and give it to theold man");
System.out.println("other: keep riding");
res=in.nextInt();
if(res == 1) {
System.out.println("Accept the money");
System.out.println("You have the coin");
coin = true;
state = 3;
} elseif (res == 2) {
etc.
Explanation / Answer
please rate - thanks did you change all the in to stdin (see red) import java.util.Scanner; public class Main{ public static void main(String[] args) { int state = 1,res; boolean coin, purse = true, horse=true; Scanner in=new Scanner(System.in); while (true) { if (state == 1){ System.out.println(" You find money and meet an oldman. What do you do? "); System.out.println("1: accept the money"); System.out.println("2: decline the money and give it to theold man"); System.out.println("other: keep riding"); System.out.println("Enter aninteger"); res =stdin.nextInt(); System.out.println(" "); if(res == 1) { System.out.println("Accept the money"); System.out.println("You have the coin"); coin = true; state = 3; } elseif (res == 2) { etc.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.