我正在为我的大学课程工作。我想对编程进行测试。我是java的新手,在你正确回答问题之后,我需要帮助来制作点数系统。我的想法是,如果你正确回答每一个问题,你都会得到一分。
我的代码:
package sandis_iesmins_kursadarbs;
import java.util.Scanner;
public class Sandis_iesmins_kursadarbs {
public static void main(String[] args) {
int a;
Scanner intScan = new Scanner(System.in);
String[] jaut = new String[3]; //jaut = is questions in latvian language
jaut[0] = "Is java a programming language";
//jaut[1] = "Kas ir prog1"; these are comments
//jaut[2] = "Kas ir prog2"; these are comments
System.out.println(jaut[0]);
String[] atbildes = {"answers", "1)Yes", "2)No", "3)Maybe", "4)Dont know"}; //answer options
for (String atb: atbildes) {
System.out.println(atb);
}
System.out.println("Insert your answers");
a = intScan.nextInt();
//answer is just "yes" and now I want to add 1 point to my score. How can I do this?
}
}
是否有任何人想要制作这种计数系统。仅供参考我将有10个大概的问题。
答案 0 :(得分:0)
您可以保留一个跟踪分数的变量。
int score = 0;
/*
Ask your question here
*/
if(/*Correct answer*/)
score++; // increment score
请查看基本的Java教程。找到一个here
答案 1 :(得分:0)
如果我理解你的问题:
放入一个名为eg的整数"计数器":
mvn clean package && mvn jar:jar failsafe:integration-test failsafe:verify
每次你想要添加的东西都可以:
int counter = 0;
在该计划中:
counter++;
答案 2 :(得分:0)
您可以使用counters
。创建名为'得分的int
变量,如果答案正确,则将其增加一点。这是一个例子:
package sandis_iesmins_kursadarbs;
import java.util.Scanner;
public class Sandis_iesmins_kursadarbs {
public static void main(String[] args) {
int a;
int score = 0;
Scanner intscan = new Scanner(System.in);
String [] jaut = new String[3]; //jaut = is questions in latvian language
jaut[0] = "Is java a programming language";
//jaut[1] = "Kas ir prog1"; these are comments
//jaut[2] = "Kas ir prog2"; these are comments
System.out.println(jaut[0]);
String[] atbildes = {"answers", "1)Yes", "2)No", "3)Maybe", "4)Dont know"}; //answer options
for(String atb: atbildes){
System.out.println(atb);
}
System.out.println("Insert your answers");
a=intscan.nextInt();
//let's imagine that the correct answer is "Yes".
if(a == 1){
score++;
}
}
不过,如果你想建立一个惩罚错误答案的系统,你可以这样做:
package sandis_iesmins_kursadarbs;
import java.util.Scanner;
public class Sandis_iesmins_kursadarbs {
public static void main(String[] args) {
int a;
double score = 0.0;
Scanner intscan = new Scanner(System.in);
String [] jaut = new String[3]; //jaut = is questions in latvian language
jaut[0] = "Is java a programming language";
//jaut[1] = "Kas ir prog1"; these are comments
//jaut[2] = "Kas ir prog2"; these are comments
System.out.println(jaut[0]);
String[] atbildes = {"answers", "1)Yes", "2)No", "3)Maybe", "4)Dont know"}; //answer options
for(String atb: atbildes){
System.out.println(atb);
}
System.out.println("Insert your answers");
a=intscan.nextInt();
//let's imagine that the correct answer is "Yes"
if(a == 1){
score++;
//the system punishes you -0.25 for wrong answer
}else{
score -= 0.25;
}
}
}