Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列有问题(涉及用户多个输入)?_Java_Arrays_User Input - Fatal编程技术网

Java 阵列有问题(涉及用户多个输入)?

Java 阵列有问题(涉及用户多个输入)?,java,arrays,user-input,Java,Arrays,User Input,无法进入食物。在第一个循环之后,程序返回到正常状态,输出我想要的结果,但是如何更改代码,以便第一个循环接收用户输入的“输入食物名称:” 提前谢谢 编辑:很抱歉事先没有看到这个问题,但是这个问题不是关于for循环的,所以如果我在下一次输入之前输入一个空字符串,我的循环将无法工作。这有什么办法吗 编辑:开始工作了。谢谢大家。您可以尝试下面的代码(这允许在用户键入结束之前输入信息)。而不是要求用户知道他们将输入多少项。此外,还创建了一个方法来处理输入。因此,您可以轻松改进、调整该方法 Enter fo

无法进入食物。在第一个循环之后,程序返回到正常状态,输出我想要的结果,但是如何更改代码,以便第一个循环接收用户输入的“输入食物名称:”

提前谢谢

编辑:很抱歉事先没有看到这个问题,但是这个问题不是关于for循环的,所以如果我在下一次输入之前输入一个空字符串,我的循环将无法工作。这有什么办法吗


编辑:开始工作了。谢谢大家。

您可以尝试下面的代码(这允许在用户键入结束之前输入信息)。而不是要求用户知道他们将输入多少项。此外,还创建了一个方法来处理输入。因此,您可以轻松改进、调整该方法

Enter food name:
(where user type his or her input)
Enter the type of this food:
(where user type his or her input)

(My problem is it prints out this instead for the first loop:)
Enter food name:
Enter the type of this food:
(where user type his or her input)
import java.io.ObjectInputStream.GetField;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类食品{
私有静态字符串[][]foodinfo;
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪键盘=新扫描仪(System.in);
foodInformation=getFoodInformation(键盘);
}
专用静态字符串[][]getFoodInformation(扫描仪输入设备){
字符串[][]结果=null;
布尔isStoping=false;
ArrayList holdName=新的ArrayList();
ArrayList holdType=新的ArrayList();
而(!isStoping){
System.out.println(“输入食品名称(或结束退出):”;
字符串foodName=inputDevice.nextLine().trim();
isStoping=“end”。相等信号案例(foodName);
如果(!isStoping){
System.out.println(“输入食品类型”);
String foodType=inputDevice.nextLine().trim();
holdName.add(foodName);
添加(foodType);
}
}
int foodCount=holdName.size();
如果(foodCount>0){
结果=新字符串[foodCount][foodCount];
对于(int i=0;i
可能重复ah很抱歉之前没有看到。循环是否有任何修复方法?这与该问题的答案相同吗?只需在
int number=keyboard.nextLine()之后写入
int number=keyboard.nextLine();
行,您就可以了。好的,我会试试。再次感谢您Aman Agnihotri。它确实有效xD。如果您愿意,您可以将它作为这个问题的答案,我会选择它作为最佳答案。谢谢您的输入。我一定会检查它。
Enter food name:
(where user type his or her input)
Enter the type of this food:
(where user type his or her input)

(My problem is it prints out this instead for the first loop:)
Enter food name:
Enter the type of this food:
(where user type his or her input)
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Scanner;

public class Foodarray {

private static String[][] foodInformation;

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner keyboard = new Scanner(System.in);
    foodInformation = getFoodInformation(keyboard);

}

private static String[][] getFoodInformation(Scanner inputDevice) {
    String[][] result = null;
    boolean isStoping = false;
    ArrayList<String> holdName = new ArrayList<String>();
    ArrayList<String> holdType = new ArrayList<String>();

    while (!isStoping) {
        System.out.println("Enter food name (or end to exit): ");
        String foodName = inputDevice.nextLine().trim();
        isStoping = "end".equalsIgnoreCase(foodName);
        if (!isStoping) {
            System.out.println("Enter food type");
            String foodType = inputDevice.nextLine().trim();
            holdName.add(foodName);
            holdType.add(foodType);
        }
    }
    int foodCount = holdName.size();
    if (foodCount>0) {
        result = new String[foodCount][foodCount];
        for (int i=0; i < foodCount; i++) {
            result[i][0] = holdName.get(i);
            result[i][1] = holdType.get(i);
        }
    }

    return result;

}

 }