英文:
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("Choose a number between 1 to 9 or please press E to exit");
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);
while(ans!= '1' || i<=5)
System.out.println("Incorrect try again!");
System.out.println("1x1:");
ans = scanner.next().charAt(0);
s--;
i++;
}
else if (ans=='1')
{
System.out.println("1x2");
}
else if(ans=='e'){
System.out.println("Thank you for playing!");
}
答案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 == 'e')
and only then System.exit()
.
public class stackTest {
public static void main(String[] args) {
System.out.println("Choose a number between 1 to 9 or exit : ");
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("Your answer : ");
int userAnw = scanner.nextInt();
int attemp = 1;
while(userAnw != value*i && attemp!=4) {
attemp++;
System.out.println("Answer Wrong!");
userAnw = scanner.nextInt();
}
if(userAnw!=value*i) System.out.println("Answer Wrong!");
else System.out.println("Answer correct!");
}
}
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 <= 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;
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论