- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我想查看条件然后移动到另一行但java中没有“goto”时,我的问题就开始了,所以我不知道如何从这个if内部移动并转到下一行当我在后面输入 return 时,它不会运行代码......如果你们能帮助我解决这个问题,我将不胜感激......源代码在这里:::
//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 " );
}
}
}
}
最佳答案
nextInt()
方法doesn't read the whole line (with carriage return and line feed) ,因此,当您重新读取 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)
关于java - 如何结束 if 并移至另一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483343/
我有一个类似于下面给出的 for 循环。 for(int i=0; i<10; i++) { boolean condition = checkCondition(); /* line 3
我有一个循环,处理文件夹的每个文件。万一文件损坏而无法打开,我想: -向用户显示错误消息 -停止执行剩余的代码 -循环到下一个文件 问题是,如果无法在循环开始时打开文件,则仍有许多代码将要执行,这将引
我知道我可以将键绑定(bind)到“移至行首”,但这会忽略缩进。 我正在寻找的是移动到一行文本的开头,以便: CGRect example = CGRectMake(view.frame.origin
我有一个用户请求,我正在努力满足,我能想到的最简单的解释方法是使用图片说明: 本质上,用户正在输入大量日期。而不是输入 MM [反斜杠(或右箭头)] DD [反斜杠] YYYY [反斜杠] 等 ...
我想打开一个 pdf 文件并移动到 C++ 和 qt 程序中的特定书签。根据我的研究,我们可以使用 URL 和命令提示符转到书签。使用 URL 具有特殊格式,如:fileName.pdf#page=3
我正在尝试移至 PHP 记录集中的下一条记录,但遇到了一些问题。 这是我的页面顶部用于设置我的记录集的内容。 $result = mysqli_query($con,"SELECT * FROM
我已经制作了一个在 java 类中运行良好的程序..但是当我将我的代码移动到 servlet 时它并没有像预期的那样运行程序创建一些文件写入它们,然后再从中读取。问题是当我将代码移动到 servlet
我遇到了在 64 位 Windows 上运行的 32 位旧版应用程序的问题。有问题的应用程序使用 CreateFileMapping 创建共享内存。当它在 64 位 Windows 上运行时,任何从另
我们计划迁移到 TFS。虽然我讨厌它,但由于各种原因我们不得不这样做。 我们有开发、暂存和实时分支。我们是否将每个分支的源代码作为单独的文件夹移至 TFS,并稍后将其转换为分支? 是否可以保留历史记录
有人可以为我提供一些关于如何将此代码移植到 renderscript 以获得更好性能的指南吗? private void someMethod() { for (int i = 0; i < src
我有一个非常大的稀疏矩阵,我想将其发送到 Java 函数。我写了一个java代码来完成这个任务,但是它很慢,所以我正在寻找更好的解决方案。我从 R 3 数组发送如下 TMPmat <- as(mySp
我正在移动一张旧的 Mantis table ,上面有 varchar(64) category_id 列添加到新的 Mantis 表中,该表的 int(10) category_id 列。 简化后的
将中等流量网站的所有读取查询从 mysql 移至 solr 是否值得?我们已经使用 solr 作为我们网站的搜索引擎。我们每 24 小时将整个记录加载到 solr 中一次,然后每 20 分钟运行部分导
我在 MySQL (innodb) 中有三个表(X、Y 和 Z)。 X是一个超过1000万行的表,并且以Y为主键作为外键。同样,Z是超过3000万行的表,并且以Y为主键作为外键。 现在的问题是 Y 的
我在没有编程经验的情况下开始探索 Swift2。 我有以下代码在 Playgrounds 中运行良好,使用快速 View 和内联 View 方法。 如何将它与 Storyboard中的实际标签相关联?
在CSAPP练习3.4中 src_t v; dest_t *p; *p = (dest_t) v; 当src_t为char且dest_t为int时,答案为 movsbl %al, (%edx) (v
我正在测试我正在构建的 Webjob 的 Poison 消息处理。 一切似乎都按预期进行,除了一件奇怪的事情: 当一条消息被移动到“-poison”队列时,它的幽灵似乎仍然隐藏在主作业队列中(不可见)
我是 Android/Java 新手,所以请耐心等待。 我从 LoginActivity > onCreate 移动了代码进入我创建的 fragment FragmentLogin方法onCreate
这只是我真实代码的演示,但问题是一样的。有没有其他或更好的解决方案。为什么 animate() 方法不起作用...... $('document').ready(function() { $('p
我有一个小工件。 HTML: X CSS: #it { background: blue; width: 40px; text-align: center; color
我是一名优秀的程序员,十分优秀!