Java 仅当/then循环为严格整数
使用Java编译一段代码,该代码接受从字符串到双精度、浮点、整数等任何输入,但只能基于整数进行处理。如果它收到一个double、float或字符串,它只会生成一条消息,提示用户重试。我不知道该怎么做,所以这是我目前的代码Java 仅当/then循环为严格整数,java,string,input,strict,Java,String,Input,Strict,使用Java编译一段代码,该代码接受从字符串到双精度、浮点、整数等任何输入,但只能基于整数进行处理。如果它收到一个double、float或字符串,它只会生成一条消息,提示用户重试。我不知道该怎么做,所以这是我目前的代码 import java.util.Scanner; public class Sand { public static void main(String[] args) { int firstInt, secondInt, thirdInt; Sca
import java.util.Scanner;
public class Sand
{
public static void main(String[] args)
{
int firstInt, secondInt, thirdInt;
Scanner keyboard = new Scanner(System.in);
System.out.println("Hi. I'm going to be asking you for three integers in just a moment.");
System.out.println("An integer is defined as a WHOLE number that's not a fraction or decimal.");
System.out.println("I swear to you, I will go off if you put in a non-whole number...");
System.out.println("Okay go ahead and type in the first of the three integers:");
if (keyboard.hasNextInt())
{
firstInt = keyboard.nextInt();
System.out.println("Red.");
System.out.println("So the first integer is " + firstInt);
System.out.println("Okay go ahead and type in the second of the three integers: ");
if (keyboard.hasNextInt())
{
secondInt = keyboard.nextInt();
System.out.println("Green.");
System.out.println("So the first and second integers are " + firstInt + " and " + secondInt);
}
else
{
System.out.println("Orange.");
System.out.println("Please try again.");
}
}
else
{
System.out.println("Blue.");
System.out.println("Please try again.");
}
}
}
我用橙色和蓝色表示有一些输入错误的时间,但它不完整。我不知道如何处理这个问题,无论是while循环、for循环还是try/catch。在学习Java方面,我是新手,所以一些笔记会对我有所帮助。该代码旨在从用户处读取字符串中的三个整数。这很简单,但分析输入是我的工作重点。这应该可以完成这项工作
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] intArray = new int[3];
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < intArray.length; i++) {
System.out.print("input integer #"+(i+1)+": ");
if (keyboard.hasNextInt()) {
intArray[i] = keyboard.nextInt();
System.out.println("value for #"+(i+1)+": " + intArray[i]);
} else
{
System.out.println("not an integer");
break;
}
}
keyboard.close();
}
}
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
int[]intArray=新的int[3];
扫描仪键盘=新扫描仪(System.in);
for(int i=0;i
或者另一个解决方案,当数字不是整数时再次询问
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
int[] intArray = new int[3];
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < intArray.length; i++) {
boolean isInputCorrect = false;
do {
System.out.print("input integer #" + (i + 1) + ": ");
if (keyboard.hasNextInt()) {
intArray[i] = keyboard.nextInt();
System.out.println("value for #" + (i + 1) + ": " + intArray[i]);
isInputCorrect = true;
} else {
System.out.println("not an integer, try again");
keyboard.nextLine();
}
} while (!isInputCorrect);
}
keyboard.close();
}
}
import java.util.Scanner;
公共类Main2{
公共静态void main(字符串[]args){
int[]intArray=新的int[3];
扫描仪键盘=新扫描仪(System.in);
for(int i=0;i
这不是要完全回答您的问题,而是要为您指明正确的方向。这里应该有足够的信息来帮助您了解您的程序还需要什么
import java.util.Scanner;
public class Sandbox {
// arguments are passed using the text field below this editor
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int num = 0;
System.out.println("enter an integer");
while (true) { //keep prompting the user until they comply
try {
num = Integer.parseInt(keyboard.nextLine());
keyboard.close();
break;
} catch (NumberFormatException e) {
System.out.println("You must enter an integer");
}
}
System.out.println("num: " + num);
}
}
一、 就个人而言,将使用数组或数组列表(根据您的需要)来存储int值,然后使用while循环来检查和接受数字。而(array.length<3)执行{int checking}。这样,如果数组中已经有3个值,脚本将不会运行,并将一直运行,直到得到3个值。“代码旨在从用户输入的字符串中读取三个整数”-您是否尝试提取用户输入的字符串中出现的前3个整数。如果输入是“1年2个水手赢了3美元”,你的程序应该提取1 2和3吗?或者输入应该是1(和)2(和)3?这对您来说可能有点高级,但是如果输入不正确,使用异常和异常处理让事务终止是处理此类问题的好方法,对于这个简单的示例来说,这可能有点高级。如果用户没有输入整数,OP似乎很难让程序接受另一个输入。此外,OP似乎是一个初学者。告诉他们在基本程序控制方面遇到困难时添加数据结构并没有多大帮助。记住,最简单的答案往往是最好的,特别是当有人第一次学习时。