Tag: java.util.scanner

摇滚,纸,剪刀游戏Java

我是编程的新手,我正在尝试用Java编写一个非常简单的Rock,Paper,Scissors游戏。 它会编译并运行正常,但我想说的是“无效的移动。再试一次。” 当用户(personPlay)没有输入正确的字符(r,p或s)时沿着这些行的某些东西。 最好的方法是什么? 例如,如果输入“q”,则应打印“无效移动”。 非常感谢你提前! // ************* // Rock.java // ************* import java.util.Scanner; import java.util.Random; public class Rock { public static void main(String[] args) { String personPlay; //User’s play — “R”, “P”, or “S” String computerPlay = “”; //Computer’s play — “R”, “P”, or “S” int computerInt; //Randomly generated number used to determine //computer’s […]

扫描程序错误,我无法弄清楚:NoSuchElementException

它在do-while循环中的第三行崩溃,并且不等待我的输入: input = kb.nextInt(); 堆栈跟踪: 线程“main”java.util.NoSuchElementException中的exception 在java.util.Scanner.throwFor(未知来源) 在java.util.Scanner.next(未知来源) 在java.util.Scanner.nextInt(未知来源) 在java.util.Scanner.nextInt(未知来源) 在main.MainDriver.main(MainDriver.java:50) 相关代码: do { displayFullMenu(); System.out.print(“Selection: “); input = kb.nextInt(); switch (input) { //Create new survey case 1: currentSurvey = new Survey(); break; //Display current survey case 2: currentSurvey.display(); break; //Save current survey case 3: saveSurvey(currentSurvey); break; //Load a survey case 4: currentSurvey = loadSurvey(); […]

连续输入命令

我的程序将以“ command parameter ”的forms读取用户键盘命令,其间有空格。 它一直执行单独的命令,直到下一个命令“ exit ”。 此外,如果用户搞砸了,程序应该显示错误但继续询问命令(我认为我没有完成的function).. 以下代码是实现此function的好方法吗? 它可以处理用户只需按下输入键w / oa命令,垃圾输入等? 如果有的话,我很想知道是否有更好的惯用方式来实现这一点。 String command = “”; String parameter = “”; Scanner dataIn = new Scanner(System.in); while (!command.equals(“exit”)) { System.out.print(“>> “); command = dataIn.next().trim(); parameter = dataIn.next().trim(); //should ^ these have error handling? if (command.equals(“dothis”)) { //do this w/ parameter.. } else if (command.equals(“dothat”)) { […]

如何检测Scanner收到的BlankLine?

我想获取数据表单文本文件,并使用Scanner获取数据表单文本文件。 它的配置文件保存模式 name status friend friend . . (Blank line) 空白行是每个配置文件分开的。(朋友将循环直到下一行是空行) john happy james james sad john 我编写代码来获取这样的文件格式文本 try{ Scanner fileIn = new Scanner(new FileReader(“testread.txt”)); while(fileIn.hasNextLine()){ String line = fileIn.nextLine(); String linename = fileIn.nextLine(); String statusline = fileIn.nextLine(); println(“name “+linename); println(“status “+statusline); while(/*I asked at this*/)){ String friendName = fileIn.nextLine(); println(“friend “+friendName); } } }catch(IOException […]

使用ASCII线处理Java IO的最快方法

我正在通过Socket使用ASCII输入/输出流,速度至关重要。 我听说使用正确的Java技术确实有所作为。 我有一本教科书说使用Buffers是最好的方法,但也建议使用DataInputStreamReader进行链接。 对于输出我使用带有OutputStreamWriter的BufferedOutputStream似乎没问题。 但我不确定输入流的用途。 我正在开发新系列,扫描仪有用吗? 速度至关重要,我需要尽快从网络上获取数据。 谢谢。 PH

Java迭代具有固定长度的数组,并使用Scanner类获取值

如何使用具有固定长度2的Scanner类获取java数组的值,并且它将迭代直到其值等于给定值? 例如; 对于以下输入, AGNH DF 我写了一个for循环来获取固定数组道路的值,其中使用Scanner类的长度为2。 for(int i = 0; i<block.length; i++){ System.out.println("enter number"); block[i]=input2.next().charAt(0); } 我想迭代这个循环,而用户输入是{‘C’,’C’}。 这意味着如果输入如下,arrays循环shpuld停止; AG NH DF CC 如何编写代码以使用Scanner类获取用户输入值并迭代数组? 并且应该复制用户输入值而不用新输入的值替换它们。 谢谢!

从文件重定向输入时出错

这是我的代码: import java.util.Scanner; class Graph{ boolean [][]array; int N; Graph (){ array = new boolean [1001][1001]; N=0; } void read_graph() { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); String str; String []substr; for (int K=1; K<=N; K++){ str = sc.nextLine(); substr = str.split(" "); for (int I=0; I0){ G.read_graph(); G.query(); numGraphs–; } } […]

未找到Scanner类java文件

扫描程序类无法找到我使用NetBeansIDE的文件,而test.txt位于文件夹路径中:D:\ netbeans project works \ ReadFile \ src \ readfile \ test.txt 在readfile.java exsist的同一文件夹中。 代码如下。 它生成未找到的文件。 package readfile; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class ReadFile { public static void main(String[] args) throws IOException , FileNotFoundException { Scanner scanner = new Scanner(new File(“test.txt”)); while (scanner.hasNextLine()) System.out.println(scanner.nextLine()); } } 输出: – run: Exception […]

Scanner的问题 – Java

我正在尝试读取包含多个单词的字符串,即。 洛杉矶或纽约市。 对于“离开”和“到达”使用scanner.next()只读取第一个,如果有两个单词并在变量之间拆分。 nextLine()也没有太大的运气。 这是我的代码: System.out.print(“\nEnter flight number: “); int flightNumber = Integer.valueOf(scanner.nextLine()); System.out.print(“\nEnter departing city: “); String departingCity = scanner.nextLine(); System.out.print(“\nEnter arrival city: “); String arrivalCity = scanner.nextLine(); 我知道这很简单,但我还没弄清楚。 这是输入/输出w /上面的代码: 输入航class号:29 输入离境城市:(立即跳到下一行) 进入抵达城市: —-我真正想要的是什么—- 输入航class号:29 进入离开城市:洛杉矶(能够输入多个单词而不跳过下一个输入) 进入抵达城市:堪萨斯城

java:读取文本文件并使用scanner类将信息存储在数组中

我有一个包含学生成绩的文本文件: Kim $ 40 $ 45 Jack $ 35 $ 40 我正在尝试从文本文件中读取此数据,并使用Scanner Class将信息存储到数组列表中。 任何人都可以指导我正确编写代码吗? 码 import java.io.*; import java.util.*; public class ReadStudentsGrade { public static void main(String[] args) throws IOException { ArrayList stuRec = new ArrayList(); File file = new File(“c:\\StudentGrade.txt”); try { Scanner scanner = new Scanner(file).useDelimiter(“$”); while (scanner.hasNextLine()) { String stuName = scanner.nextLine(); […]