刚开始教自己如何编写java。第一部分是一个简单的数学方程式,用户必须找到'n'的值。有一个'if'或'else',如果答案是正确的(21)我如何绕过else并运行程序的其余部分,如果'else'如何在两次尝试后终止程序?
import java.util.Scanner;
import java.util.Random;
public class Bot {
public static void main(String[] args) {
System.out.println("(6*7)%2=n");
int InQuestion;
System.out.println("what is the value of ' n' in the equation");
Scanner ScannedNumber = new Scanner (System.in);
int n = ScannedNumber.nextInt();
InQuestion= n;
if (InQuestion==21)
System.out.println("Access Allowed");
//*If 'Access Allowed' then permit the rest of the program to run consequently
//*passing the 'else' branch.
else
System.out.println("try again");
for(n=0; n>2; n++);{
int n1=ScannedNumber.nextInt();
if (n1==21);
System.out.println("Access Denied");
System.exit(0);
}
System.out.println("Hello I Am Bot");
System.out.println("What is Your Name?");
Scanner VariableNamePerry=new Scanner(System.in);
System.out.print("Hello," +VariableNamePerry.nextLine());
Scanner VariableNamecalculate=new Scanner (System.in);
System.out.println(" What can I do for you?");
System.out.print(VariableNamecalculate.nextLine());
System.out.print(" what?");
Scanner VariableNameThatIsWhatIWantYouToDo=new Scanner (System.in);
System.out.println("I Can Calculate The Square Of Any Number, If that Is What You Want?" +VariableNameThatIsWhatIWantYouToDo.nextLine());
Scanner VariableNameOkay=new Scanner (System.in);
System.out.println(VariableNameOkay.nextLine()+ (" then"));
System.out.println("Give me a number...");
Scanner VariableName1=new Scanner (System.in);
for(int MyVar=0; MyVar<1000; MyVar++){
int MyVar1 = VariableName1.nextInt();
System.out.println(MyVar1*MyVar1);
System.out.println("give me another number please?")
}
}
}
答案 0 :(得分:0)
您希望在if和else语句周围添加块以限定它们。如果你想在两次尝试后停止,你可以使用while循环或while循环,并使用一个简单的计数器计算尝试次数,并且当它们错误时递增计数器。
答案 1 :(得分:0)
您可以使用循环遍历代码的开头部分,直到授予访问权限或者他们的尝试用完为止。如果已经授予访问权限,那么在打开循环时只需使用某种指示符。
int attempts = 2;
boolean accessGranted = false;
for (int q = 0; q < attempts; q++){
System.out.println("(6*7)%2=n");
int InQuestion;
System.out.println("what is the value of ' n' in the equation");
Scanner ScannedNumber = new Scanner (System.in);
int n = ScannedNumber.nextInt();
InQuestion= n;
if (InQuestion==21){
System.out.println("Access Allowed");
accessGranted = true;
break;
}
else
System.out.println("try again");
}
if (accessGranted){/*rest of program*/}
答案 2 :(得分:0)
对于程序的继续,最好的方法是将实际的程序代码放在与main不同的空白处。
以下是一个例子:
public static void main(String[] args) {
System.out.println("(6*7)%2=n");
int InQuestion;
System.out.println("what is the value of ' n' in the equation");
Scanner ScannedNumber = new Scanner (System.in);
int n = ScannedNumber.nextInt();
InQuestion= n;
if (InQuestion==21){
System.out.println("Access Allowed");
program(args);
//*If 'Access Allowed' then permit the rest of the program to run consequently
//*passing the 'else' branch.
} else {
System.out.println("try again");
for(n=0; n>2; n++){
int n1=ScannedNumber.nextInt();
if (n1==21);
System.out.println("Access Denied");
System.exit(0);
}
}
}
public static void program(String[] args){
System.out.println("Hello I Am Bot");
System.out.println("What is Your Name?");
Scanner VariableNamePerry=new Scanner(System.in);
System.out.print("Hello," +VariableNamePerry.nextLine());
Scanner VariableNamecalculate=new Scanner (System.in);
System.out.println(" What can I do for you?");
System.out.print(VariableNamecalculate.nextLine());
System.out.print(" what?");
Scanner VariableNameThatIsWhatIWantYouToDo=new Scanner (System.in);
System.out.println("I Can Calculate The Square Of Any Number, If that Is What You Want?" +VariableNameThatIsWhatIWantYouToDo.nextLine());
Scanner VariableNameOkay=new Scanner (System.in);
System.out.println(VariableNameOkay.nextLine()+ (" then"));
System.out.println("Give me a number...");
Scanner VariableName1=new Scanner (System.in);
for(int MyVar=0; MyVar<1000; MyVar++){
int MyVar1 = VariableName1.nextInt();
System.out.println(MyVar1*MyVar1);
System.out.println("give me another number please?");
}
}
像Nkdy说的那样,你可以使用while循环添加到int,直到达到指定的限制。