我正在研究一个非常简单的Java问题,我需要在输入某个用户值后停止接收数据。输入值后,用户仍然可以输入数据,但输入值后,输出不应包含该值或该值之后的任何数据。此外,我试图打消负数条目,但无济于事。这是我的代码。任何帮助,将不胜感激!谢谢!
import java.util.Scanner;
public class Sample
{
public static void main (String[] args)
{
System.out.println("Please input any number, up to 10 entries. "
+ "Enter -1 to exit: ");
Scanner read = new Scanner(System.in);
int[] input_array = new int[10];
int numOfEntries = 0;
for(int i = 0; i < input_array.length; i++)
{
input_array[i] = read.nextInt();
numOfEntries++;
//only positive numbers
if(input_array[i] >= 100 || input_array[i] < 0)
{
if(input_array[i] == 77) //certain value
{
//entries before 77 is entered become the new array
numOfEntries = i;
int[] new_array = new int[numOfEntries];
for (int j = 0; j < numOfEntries; j++)
{
new_array[j] = input_array[i];
}
System.out.print(new_array[numOfEntries] + " ");
}
if(input_array[i] == -1) //exit loop
{
break;
}
System.out.println("Please enter a positive
number that is less than 100.");
}
}
//final result
System.out.println("Your entries are: ");
for (int i = 0; i < numOfEntries; i++)
{
System.out.print(input_array[i] + " ");
}
答案 0 :(得分:0)
您应该像这样检查以确保一切正确通过条件
//all this goes inside the for loop
String input = read.nextInt();
if (input == -1)
break;
if (input > 0)//is it negative?
{
if (input < 100)//is it greater than 100?
{
if (input == 77)//is it 77?
{
//your code
}
else//this will run when all conditions are passed
input_array[i] = input;
}
}
答案 1 :(得分:0)
无需创建单独的数组,只有当它是有效值时才添加到初始数组中。检查以下代码,进行了一些修改:
public static void main(String[] args) throws Exception {
{
System.out.println("Please input any number, up to 10 entries. " + "Enter -1 to exit: ");
Scanner read = new Scanner(System.in);
int[] input_array = new int[10];
int numOfEntries = 0;
int entry = 0, status = 0;
for (int i = 0; i < input_array.length; i++) {
entry = read.nextInt();
// only positive numbers
if (entry < 100 && entry >= 0) {
if (entry == 77) // certain value,change the value of 'status' here
{
status = 1;
}
if (status == 0) //if 'status' is not reset,then add the entry to array.
{
input_array[numOfEntries++] = entry;
}
} else if (entry == -1) // exit loop
{
break;
}else {//for negative and values which are greater than 100.
System.out.println("Please enter a positive number that is less than 100.");
}
}
// final result
System.out.println("Your entries are: ");
for (int i = 0; i < numOfEntries; i++) {
System.out.print(input_array[i] + " ");
}
}
}
答案 2 :(得分:0)
如果你不打算使用它,你不需要添加另一个数组,所以我删除了它并在你的代码中进行了一些修改。请检查以下代码。仔细阅读评论。希望你能理解。
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
System.out.println("Please input any number, up to 10 entries. "
+ "Enter -1 to exit: ");
int[] input_array = new int[10]; //Input array - size 10
int input = 0; // input variable
boolean isInput77 = false; //Check input is 77 (certain value)
int numOfEntries = 0; // Number of entries that passes your conditions
Scanner read = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
input = read.nextInt(); // input value assign to "input" variables
if (input == -1){ // input is -1 break
break;
} else if (input == 77){ // input is 77 isInput77 is true
isInput77 = true;
} else if (!isInput77 && input < 100 && input >= 0) { // isInput77 is false and input is positive number that is less than 100 add to input_array
input_array[numOfEntries] = input;
numOfEntries++;
} else if(input >= 100 || input < -1){ // input is negative or greater than or equal to 100
System.out.println("Please enter a positive number that is less than 100.");
}
}
System.out.println("Your entries are: ");
for (int i = 0; i < numOfEntries; i++) { // Print only valuable entries
System.out.print(input_array[i] + " ");
}
}
}