我试图使用Scanner类使用下一个(模式模式)方法读取一行,以捕获冒号前的文本,然后捕获冒号后的文本,以便s1 = textbeforecolon和s2 = textaftercolon.
这条线看起来像这样:
东西:somethingelse
我一直在无限循环中捕获我的代码.
它没什么可提升的,但我无法理解我的生活!
有人请帮忙
我只是重新创建了特定的错误,没有我在实际程序中的所有if语句.
package bs;
import java.util.InputMismatchException;
import java.util.Scanner;
public class bs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean continueVar = true;
while (continueVar) {
try {
System.out.println("Enter Something");
int input = sc.nextInt();
} catch (InputMismatchException i) {
System.out.println("What the f***?");
continueVar = true;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
捕获输入不匹配异常时会发生无限循环.我认为它至少会要求用户重新输入他们的输入,但不是这样做,而是继续循环,如下所示:
run:
Enter Something
df
What the f***?
Enter Something
What the f***?
Enter Something
What the f***?
Run Code Online (Sandbox Code Playgroud)
它的行为就像只是忽略扫描仪对象sc?!
java exception-handling try-catch infinite-loop java.util.scanner
我试图弄清楚用户是否有办法在一行中输入两个值并将每个值放在一个单独的变量中.
例如,我有一个整数变量"x"和一个整数变量"y".我提示用户说:"输入x和y坐标:".让我们说用户输入:"1 4".如何扫描x = 1和y = 4?
我是初学者,通过回答过去的考试试卷中的问题来学习java和修改我的考试,还有一个问题我被困在了.
考虑下面的代码片段,它读取输入命令然后处理它.
String cmd = scanner.next();
if (cmd == "forward" )
robot.forward(1);
else if (cmd == "turn" )
robot.turn();
else
System.out.println("Unknown command: " + cmd);
Run Code Online (Sandbox Code Playgroud)
在测试程序时,扫描程序将字符串"forward"读入cmd,但程序输出"Unknown command:forward".
a)详细解释为什么会发生这种情况.
b)应该对代码进行哪些更改以更正此错误.
如果有人可以帮我回答问题a)和b)我将不胜感激.
ps我明白这不是一个只寻找答案的网站(#noeasywayout)所以我会尽量不要在这里贪婪.对于给您带来的任何不便,我深表歉意.
我试图读取用户输入的信用卡号码.但是输入10位后输入输入错误.任何低于10位的数字都适用于我的代码.
do{
System.out.print("Please enter your creditcard number:");
ccNum = scan4.nextInt();
int length = String.valueOf(ccNum).length();
if(length !=12)
{
ccNumInfo=false;
System.out.println("Please enter a 12 digit card number");
}
}while(ccNumInfo!= true);
Run Code Online (Sandbox Code Playgroud) 所以我试图在Java中创建一个简单的程序来读取文本文件(来自命令行参数),用户可以检查他们输入的数字是否在文本文件中.
File inputFile = new File(args[0]);
Scanner scanman = new Scanner(inputFile); //Scans the input file
Scanner scanman_2 = new Scanner(System.in); //Scans for keyboard input
int storage[] = new int[30]; //Will be used to store the numbers from the .txt file
for(int i=0; i<storage.length; i++) {
storage[i]=scanman.nextInt();
}
System.out.println("WELCOME TO THE NUMERICAL DATABASE"+
"\nTO CHECK TO SEE IF YOUR NUMBER IN THE DATABASE"+
"\nPLEASE ENTER IT BELOW! TO QUIT: HIT CTRL+Z!");
while(scanman_2.hasNext()){
int num_store = scanman_2.nextInt();
boolean alert = false;
for …Run Code Online (Sandbox Code Playgroud) 我正在上一个在线MOOC学习Java,我唯一的问题是它来自芬兰的赫尔辛基大学,我住在美国所以在有限的时间我可以醒来在练习上寻求帮助.我目前的做法是向用户询问一个数字,然后在使用时将每个整数打印到该数字
while {
}
Run Code Online (Sandbox Code Playgroud)
声明
这是我目前的代码
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int number = Integer.parseInt(reader.nextLine());
System.out.print("up to what number?:"+ number);
while (number<=number){
System.out.println(number);
number++;
}
}
Run Code Online (Sandbox Code Playgroud)
而它看起来正在做的是忽略了
while (number<=number) {
System.out.println(number);
Run Code Online (Sandbox Code Playgroud)
我的部分代码并直接进入
number++;
Run Code Online (Sandbox Code Playgroud)
我的代码的一部分我需要声明另一个int(变量)来存储值吗?课程具有评分测试用例的方式我不能简单地声明具有确定值的变量,因为它们运行几个测试用例,如正数和负数.有没有办法使用阅读器将值存储到两个单独的变量,以便它们可以相互比较,只打印数字到那个数字?
我也知道我错过了一个
Break;
Run Code Online (Sandbox Code Playgroud)
声明,但我不知道我会把它放在我有的代码中,我试图使用
} else {
break;
Run Code Online (Sandbox Code Playgroud)
但得到一个错误,说明我有一个没有if的别人.我正在使用netbeans,因为它是我的课程所必需的,因为服务器提交是通过TMC设置的.
考虑到它现在我确定它不会跳过while语句但只是继续打印因为它打印和增量用户输入也增加了因为我只有一个变量但我不知道我怎么会去将用户输入值存储在两个不同的变量中,我可以将它们与小于或等于语句进行比较,并在达到用户输入的数字后停止打印,在这种情况下,我不一定需要break语句,因为它会一旦打印到数字输入就停止.
回答:这是我最终想出的答案.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("up to what number?:");
int numbers = 1;
int number = Integer.parseInt(reader.nextLine());
while (numbers <= number){
System.out.println(numbers);
numbers++;
} …Run Code Online (Sandbox Code Playgroud) 所以我希望我的程序读取一个输入,它在一行中有一些整数,例如:
1 1 2
然后它应该分别读取每个整数并以新行打印它.程序必须读取的整数数量不是事先给出的,所以我要做的是使用while循环,在没有更多的整数读取之后结束.这是我写的代码:
while (scan.hasNextInt()) {
int x = scan.nextInt();
System.out.println(x);
}
Run Code Online (Sandbox Code Playgroud)
但它没有正常工作,因为循环永远不会结束,它只是希望用户输入更多的整数.我在这里错过了什么?
自从2年生锈以来,我一直在从头学习Java,我正在玩一个简单的随机生成器代码.我的问题是当用户被问到他想要什么作为他的最高掷骰时,它必须是数字(int)类类型.
我试图创建一个if语句并将变量与其类进行比较,而不是让我的IDE停止并在用户键入字母的情况下向我显示错误消息.
这是我的代码(这是有史以来最简单的代码,但可以肯定地说我是新手并且激励自己再次学习Java.):
package firstguy;
import java.util.Random;
import java.util.Scanner;
public class randomnum {
public static void main(String[] args){
Random dice = new Random();
Scanner userin = new Scanner(System.in);
int number;
int highnum;
System.out.println("What's the highest roll you want? \n");
highnum = userin.nextInt();
for(int counter=1; counter<= highnum; counter++){
number= 1 + dice.nextInt(highnum);
System.out.println("This is the number " + number);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能够比较highnum,在这里看它是否作为类类型int而不是字母.如果键入字母或字符,则应显示消息或应重复该问题.我一直在努力寻找这个问题,但我不断得到比较同一类类型的两个变量的结果.
有没有办法将变量与类类型进行比较?
我什么时候运行以下代码,
private static String input(String Message){
Scanner input_scanner = new Scanner(System.in);
System.out.print("\n" + Message);
String string = input_scanner.nextLine();
input_scanner.close();
return string;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at main.learn.input(learn.java:25)
at main.learn.main(learn.java:13)
Run Code Online (Sandbox Code Playgroud)
我发现这与线路有关input_scanner.close();,但当我将其移除时,我会收到警告说:
资源泄漏:"input_scanner"永远不会关闭"
无论如何,我可以阻止错误发生并摆脱警告?