gpt4 book ai didi

java - 让用户输入创建对象

转载 作者:行者123 更新时间:2023-12-01 10:16:18 25 4
gpt4 key购买 nike

我正在编写这个程序,用户可以在其中进行数学测试。我现在面临并一直试图解决的问题是如何在测试结束时显示最终分数。

我有一个计数器,每个正确答案的分数都会增加,但我如何(最后)显示最终/总分?如有任何帮助,我们将不胜感激。

顺便说一句,这是三个类之一。

import java.util.Scanner;

public class Questions {

Scanner scan = new Scanner(System.in);

int answer = 0;
int point = 0;
String correct = "Correct";

public void pointers(){
point++;
System.out.println(point + " point added to total score");
}

public void totalPoints(){
pointers();
}

public void showQuestions(){

System.out.println("\n--------------\nQuestion #1: 1+1 = ? ");
answer = scan.nextInt();

if(answer == 2){
System.out.println(correct);
pointers();
}else{
System.out.println("Wrong!");
}

System.out.println("\nQuestion #2: 340-23 = ? ");
answer = scan.nextInt();
if(answer == 317){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}

System.out.println("\nQuestion #3: 900/3 = ? ");
answer = scan.nextInt();
if(answer == 300){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}

System.out.println("\nQuestion #4: 23*2 = ? ");
answer = scan.nextInt();
if(answer == 46){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}

System.out.println("\nQuestion #5: -4+6 = ? ");
answer = scan.nextInt();
if(answer == 2){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}


}
}

最佳答案

问题是您首先尝试获取输入,然后询问年龄。当您键入 Keyboard.nextInt() 时,它会立即等待输入。那么问题来了:

    *int age = keyboard.nextInt();
* System.out.println("Age: " + age);
*age = keyboard.nextInt();

我的建议是删除第一个keyboard.nextInt():例子: System.out.println("\n姓名: "+ 姓名); 名称 = 键盘.nextLine();

    System.out.println("Age: " + age);
final int age = keyboard.nextInt();

仅此而已。请注意方法调用的位置,例如它们应该在你的 println() 之后。

关于java - 让用户输入创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35867695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com