Java 如何结束if并移动到另一行
当我想看到条件,然后移动到另一行时,我的问题就开始了,但是java中没有“goto”,所以我不知道如何从if内部移动,然后在返回之后再移动到下一行,它不会运行代码。。。。 如果你们能帮我解决这个问题我会很感激。。。 源代码如下:Java 如何结束if并移动到另一行,java,Java,当我想看到条件,然后移动到另一行时,我的问题就开始了,但是java中没有“goto”,所以我不知道如何从if内部移动,然后在返回之后再移动到下一行,它不会运行代码。。。。 如果你们能帮我解决这个问题我会很感激。。。 源代码如下: //package CaloriesCalculator import java.util.Scanner; class Main { // @SuppressWarnings("null") @SuppressWarnings("resource") publ
//package CaloriesCalculator
import java.util.Scanner;
class Main {
// @SuppressWarnings("null")
@SuppressWarnings("resource")
public static void main(String[] args) {
// Initiate a new Scanner
// here I have tried to use scanner
boolean run = true;
while(run){
@SuppressWarnings("resource")
Scanner userInputScanner = new Scanner(System.in);
System.out.print("\nIf you are Male Press 1, If you are Female Press 2 ");
int G = userInputScanner.nextInt();
if(G == 1)
{
System.out.print("Please Enter your Weight..");
int W = userInputScanner.nextInt();
if((W <300) && (W > 1)) {//error message
// my problems start here !! I need to have the next input here but it doesn't work
}
System.out.print("Please Enter your Weight..");
W= userInputScanner.nextInt();
return;
// int m;
System.out.print("Please Enter your Height");
int H = userInputScanner.nextInt();
System.out.print("Please Enter your Age..");
int A = userInputScanner.nextInt();
double BMR = 10*W + 6.25*H - 5*A +5 ;
if( BMR > 0) {System.out.print(" Your body needs the amount of calories : "+ BMR );}
else { System.out.print("Do you know you are a mess ? " );}
//int H = userInputScanner.nextInt();
// else {System.out.print("Please Enter valid weight");}
}
else if(G == 2)
{
System.out.print("Please Enter your Weight..");
int W = userInputScanner.nextInt();
System.out.print("Please Enter your Height");
int H = userInputScanner.nextInt();
System.out.print("Please Enter your Age..");
int A = userInputScanner.nextInt();
double BMR = 10*W + 6.25*H - 5*A -161 ;
if(BMR > 0)
{
System.out.print(" Your body needs the amount of calories : "+ BMR );
}
else
{
System.out.print("Do you know you are a mess ? " );
}
}
else
{
System.out.print(" Please put the right number or don't use program " );
}
}
}
//包装热量计算器
导入java.util.Scanner;
班长{
//@SuppressWarnings(“空”)
@抑制警告(“资源”)
公共静态void main(字符串[]args){
//启动一个新的扫描仪
//在这里,我尝试使用扫描仪
布尔运行=真;
while(运行){
@抑制警告(“资源”)
Scanner userInputScanner=新扫描仪(System.in);
System.out.print(“\n如果您是男按1,如果您是女按2”);
int G=userInputScanner.nextInt();
如果(G==1)
{
系统输出打印(“请输入您的体重…”);
int W=userInputScanner.nextInt();
如果((W 1)){//错误消息
//我的问题从这里开始!!我需要在这里输入下一个内容,但它不起作用
}
系统输出打印(“请输入您的体重…”);
W=userInputScanner.nextInt();
返回;
//int m;
系统输出打印(“请输入您的身高”);
int H=userInputScanner.nextInt();
系统输出打印(“请输入您的年龄…”);
int A=userInputScanner.nextInt();
双BMR=10*W+6.25*H-5*A+5;
如果(BMR>0){System.out.print(“你的身体需要热量:“+BMR”);}
else{System.out.print(“你知道你是一团糟吗?”);}
//int H=userInputScanner.nextInt();
//else{System.out.print(“请输入有效重量”);}
}
else如果(G==2)
{
系统输出打印(“请输入您的体重…”);
int W=userInputScanner.nextInt();
系统输出打印(“请输入您的身高”);
int H=userInputScanner.nextInt();
系统输出打印(“请输入您的年龄…”);
int A=userInputScanner.nextInt();
双BMR=10*W+6.25*H-5*A-161;
如果(BMR>0)
{
System.out.print(“你的身体需要热量:“+BMR”);
}
其他的
{
System.out.print(“你知道自己一团糟吗?”);
}
}
其他的
{
系统输出打印(“请输入正确的号码或不要使用程序”);
}
}
}
}方法,因此,当您重新读取nextInt()
时,您会得到一个空字符串,这就是if不执行其中代码的原因
你必须:
nextLine()
阅读整行内容Integer.parseInt()
方法验证整数输入Scanner userInputScanner = new Scanner(System.in);
String s = userInputScanner.nextLine();
try{
Integer.parseInt(s);
}
catch(NumberFormatException ex){
System.out.println("Its not a valid Integer");
}
否则,您可以先执行nextInt()
,然后执行nextLine()
来使用\n
:
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
tnx的帮助,但我无法使它工作。。我太初学JAVa了,无法理解这段代码。。。我找不到合适的位置,或者即使找到了,我也会犯很多其他错误…:(太遗憾了!请尝试完全相同的代码,只需在
int G=userInputScanner.nextLine();
和int W=userInputScanner.nextLine();
之后添加一行userInputScanner.nextLine();
(或者每次使用nextInt()
)