gpt4 book ai didi

java - 扫描仪 next() 输入不匹配异常 ..在 nextInt() 之后使用 nextLine()

转载 作者:行者123 更新时间:2023-12-01 22:11:56 25 4
gpt4 key购买 nike

我找不到错误。我用过nextLine()读完nextInt()但仍然next()没有读到这个词(“更新”)。我错过了什么?

输入阅读内容:

    2
4 5
UPDATE 2 2 2 4
QUERY 1 1 1 3 3 3
UPDATE 1 1 1 23
QUERY 2 2 2 4 4 4
QUERY 1 1 1 3 3 3
2 4
UPDATE 2 2 2 1
QUERY 1 1 1 1 1 1
QUERY 1 1 1 2 2 2
QUERY 2 2 2 2 2 2

我的代码:

   Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0 ; i<t ; i++){
int n = sc.nextInt();
int m = sc.nextInt();
sc. nextLine ();
System.out.println(n+ " " + m);
for(int j=0 ; j<m ; j++){
String Q = sc.next(); // tried (String)sc.next(); also

if(Q == "UPDATE"){
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int val = sc.nextInt();
sc.nextLine();
System.out.println(x+ " " + y + " "+ z + " "+ val);
}
else if(Q == "QUERY"){
int x1 = sc.nextInt()-1;
int y1 = sc.nextInt()-1;
int z1 = sc.nextInt()-1;

int x2 = sc.nextInt();
int y2 = sc.nextInt();
int z2 = sc.nextInt();
sc.nextLine();

System.out.println(x1+ " " + y1 + " "+ z1 + " "+ x2+ " " + y2 + " "+ z2);
}


}
}

最佳答案

java中的字符串比较是通过.equals进行的

替换您的代码

if(Q.equals("UPDATE")){

关于java - 扫描仪 next() 输入不匹配异常 ..在 nextInt() 之后使用 nextLine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com