你好,我是新手,我应该制作一个允许我循环提问的 Java 程序吗?

huangapple go评论90阅读模式
英文:

Hi, Im new, do I make a java program that allows me to loop a question?

问题

System.out.println("选择一个从1到9的数字,或按E退出程序。");
Scanner scanner = new Scanner(System.in);
char num = scanner.next().charAt(0);

switch (num) {
    case '1':
        System.out.println("1x1:");
        char ans;
        ans = scanner.next().charAt(0);

        if (ans != '1') {
            System.out.println("1x1:");
            ans = scanner.next().charAt(0);

            int i = 1;
            while (ans != '1' && i <= 4) {
                System.out.println("回答错误,请重试!");
                System.out.println("1x1:");
                ans = scanner.next().charAt(0);
                i++;
            }

            if (ans == '1') {
                System.out.println("1x2:");
            }
        } else if (ans == '1') {
            System.out.println("1x2:");
        } else if (ans == 'e') {
            System.out.println("谢谢参与!");
        }
        break;
}

请注意,代码中只翻译了您提供的部分内容。如果您还有其他代码部分需要翻译,请提供相关内容。

英文:

Im trying to make a program that can do the following.

Select a number from one to nine or press e to exit program.

for example I choose number 1, It would make me a simple multiplication test like "1x1:" up to "1x10:" if I choose two it would ask "2x1" up to 2x10" each question would loop up to 4 attempts if you get the wrong answer or move to the next question if you get it right. How do I make it loop and my if methods doesn't work. please help.

    System.out.println(&quot;Choose a number between 1 to 9 or please press E to exit&quot;);
    Scanner scanner = new Scanner(System.in);
    char num = scanner.next().charAt(0);

     
   
    switch (num) {

        case &#39;1&#39;:
       System.out.println(&quot;1x1&quot;);
             char ans;
            ans = scanner.next().charAt(0);
       
       if(ans!= &#39;1&#39;)
       {
              System.out.println(&quot;1x1&quot;);
              
            ans = scanner.next().charAt(0);
            
           while(ans!= &#39;1&#39; || i&lt;=5)
           System.out.println(&quot;Incorrect try again!&quot;);
            System.out.println(&quot;1x1:&quot;);
            
            ans = scanner.next().charAt(0);
       s--;
     i++;
       }
       
       else if (ans==&#39;1&#39;)
       {
        System.out.println(&quot;1x2&quot;);
       }
           
       
       else if(ans==&#39;e&#39;){
       System.out.println(&quot;Thank you for playing!&quot;);
        }

答案1

得分: 0

我假设当用户输入任何字母时程序应该退出。但你可以将它更改为else if(num == 'e'),然后才执行System.exit()

public class stackTest {
    public static void main(String[] args) {
        System.out.println("选择一个数字,范围在1到9之间,或输入 'e' 退出程序:");
        Scanner scanner = new Scanner(System.in);
        char num = scanner.next().charAt(0);
        Boolean check = Character.isDigit(num);
        if (check) {
            int value = Character.getNumericValue(num);
            for (int i = 1; i <= 10; i++) {
                System.out.println(num + "x" + i);
                System.out.println("你的答案:");
                int userAnw = scanner.nextInt();
                int attempt = 1;

                while (userAnw != value * i && attempt != 4) {
                    attempt++;
                    System.out.println("回答错误!");
                    userAnw = scanner.nextInt();
                }
                if (userAnw != value * i) System.out.println("回答错误!");
                else System.out.println("回答正确!");
            }
        } else if (num == 'e') {
            System.exit(0);
        }
    }
}

请注意,我只翻译了代码部分,没有其他内容。

英文:

I just assume when user enters any letter it should exit the program. But you can change it to else if(num == &#39;e&#39;) and only then System.exit().

public class stackTest {
	public static void main(String[] args) {
		System.out.println(&quot;Choose a number between 1 to 9 or exit : &quot;);
		Scanner scanner = new Scanner(System.in);
        char num = scanner.next().charAt(0);
        Boolean check = Character.isDigit(num);
         if(check) {
            int value=Character.getNumericValue(num); 
            for(int i=1 ; i&lt;=10 ; i++){
                System.out.println(num + &quot;x&quot; + i);
                System.out.println(&quot;Your answer : &quot;);
                int userAnw = scanner.nextInt();
                int attemp = 1;
                
                while(userAnw != value*i &amp;&amp; attemp!=4) {
                	attemp++;
                	System.out.println(&quot;Answer Wrong!&quot;);
                	userAnw = scanner.nextInt();
                }
                if(userAnw!=value*i) System.out.println(&quot;Answer Wrong!&quot;);
                else System.out.println(&quot;Answer correct!&quot;);
            }
         }
         else System.exit(0);
	}
}

答案2

得分: -1

我认为它的工作方式相当清楚但如果你有问题就问吧

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                System.out.print(String.format("%d * %d = ", i, j));
                int response = scanner.nextInt();
                int fails = 1;

                while (response != i * j && fails < 4) {
                    fails++;
                    System.out.print(String.format("%d * %d = ", i, j));
                    response = scanner.nextInt();
                }

                if (fails == 4) {
                    System.out.println("You failed!");
                    return;
                }
            }
        }
    }
}
英文:

I think it's pretty clear how it works but if you have a question go ahead.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	    Scanner scanner = new Scanner(System.in);
	    
	    for (int i = 1; i &lt;= 10; i++) {
	        for (int j = 1; j &lt;= 10; j++) {
                System.out.print(String.format(&quot;%d * %d = &quot;, i, j));
                int response = scanner.nextInt();
                int fails = 1;

                while (response != i * j &amp;&amp; fails &lt; 4) {
                    fails++;
                    System.out.print(String.format(&quot;%d * %d = &quot;, i, j));
                    response = scanner.nextInt();
                }

                if (fails == 4) {
                    System.out.println(&quot;You failed!&quot;);
                    return;
                }
            }
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月22日 07:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64473111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定