这个代码正在推动我的债券!我花了一个星期的时间试图找出问题所在.事实上,我的教师也无法在代码中找到问题 - 因此非常令人沮丧...特别是因为它不会因为假定的解析问题而编译.
任何帮助都会令人惊叹,非常感谢!! (如果不是太麻烦,你可以向我解释你为什么要进行调整吗?)
import java.util.Scanner;
public class ChooseYourOwnAdventure
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String Wh, Kt, yn, WH2, ny;
System.out.println("WELCOME TO MITCHELL'S TINY ADVENTURE!");
System.out.println("You are in a creepy house! Would you like to go \"upstairs\" or into the \"kitchen\"?");
Wh = keyboard.next();
{
if (Wh.equals("kitchen"))
{
System.out.println( "There is a long counter top with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Scanner对象将字符串输入写入文本文件.
字符串输入是电影名称.但是,如果文件名有两个单词,则扫描程序对象只接受第一个单词.
我需要它来接受这两个词.这是我的代码: -
Scanner new_dvd_info = new Scanner(System.in);
System.out.println("Enter name of new film");`
String film_name = new_dvd_info.next();
Run Code Online (Sandbox Code Playgroud)
请问有人放光吗?
我必须根据数字从用户读取字符串n并n在n不同变量中存储字符串.我坚持如何将它们放入不同的字符串中.请帮帮我.
这是我的代码:
public static void main(String[] args) {
int b;
String s="";
Scanner in = new Scanner(System.in);
System.out.println("Enter verifying number: ");
b = in.nextInt();
for (int i=0; i<=b; i++) {
System.out.println("Enter a string: ");
s = in.nextLine();
}
Run Code Online (Sandbox Code Playgroud)
因此,如果b = 5,我必须从用户输入5个字符串并将它们存储在5个不同的字符串变量中.我能够从用户那里获取它但不能将它们分配到不同的变量中.你可以帮帮我吗?谢谢.
我一直在互联网上搜索,似乎找不到这个答案 那么有谁知道如何阻止用户输入只允许数字的字母?
这是我的代码到目前为止的样子。
public static double payCalculator(double hours, double basePay)
{
double totalPay;
double overTime = 8.00 * 1.5;
while(hours < 0 | hours > 60) {
System.out.println("Cannot work more than 60 hours a week");
System.out.println("Hours Work?");
hours = in.nextDouble();
}
while(basePay < 8) {
System.out.println("Base Pay cannot be less than 8");
System.out.println("Base Pay?");
basePay = in.nextDouble();
}
if
(hours <= 40){
totalPay = hours*basePay;
}
else {
totalPay = ((hours - 40)*overTime) + (40*basePay);
}
return totalPay;
}
public …Run Code Online (Sandbox Code Playgroud) 例1:
System.out.print("Enter an integer: ");
int x = input.nextInt();
if(x < value) { }
Run Code Online (Sandbox Code Playgroud)
例2:
System.out.print("Enter an integer: ");
if(input.nextInt() < value) { }
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
如果您只需要特定条件下的值,哪一个更好用?或者两种语法在内存方面都相同?
在内存方面,第二个例子中输入的值是怎么回事?
请详细说明我的答案.
使用try(Scanner scan = new Scanner(System.in)) { }导致
线程“main”中的异常 java.util.NoSuchElementException
当我尝试调试时,它说
变量信息不可用,源代码编译时没有 -g 选项。
并显示以下代码
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
Run Code Online (Sandbox Code Playgroud)
我使用这一行的方法之一:
protected String loginName(){
String username;
String password;
try (Scanner scan = new Scanner(System.in)) { // This line is causing the error.
System.out.print("Enter Username: ");
username = scan.next();
System.out.print("Enter Password: ");
password = scan.next();
}
if(getUsernamesList().contains(username))
if(password.equals(getPasswordsList().get(getUsernamesList().indexOf(username)))) return username;
else return "-1";
else return "-1";
}
Run Code Online (Sandbox Code Playgroud) 每当用户输入为1或Jan时,代码都不会读取并跳转到第二个语句并显示
已输入无效月份
Scanner in = new Scanner(System.in);
System.out.println("Enter a month: ");
String month=in.nextLine();
if((month == "1") || (month == "Jan")){
System.out.println("Month: January");
}
else{
System.out.println("Invalid month has been entered");
Run Code Online (Sandbox Code Playgroud) 尝试编译
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt()++;
}
}
Run Code Online (Sandbox Code Playgroud)
给出错误java: unexpected type; required: variable; found: value。
使用+ 1而不是++解决此错误。
我知道区别是“将变量的值加1”和“将值增加1”。
但是我从没碰到能带来改变的代码。有人可以解释吗?
所以我正在使用这个编译器站点处理我的代码:https : //www.tutorialspoint.com/compile_java_online.php
当我运行我的代码时,它出现了这个错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at WorkingWithLoops.main(WorkingWithLoops.java:22)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import java.util.Scanner;
public class WorkingWithLoops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int negative = 0, positive = 0, sum = 0, value;
System.out.print("Enter a value: ");
value = input.nextInt();
while (value != 0){
if (value < 0){
negative++;
}
else {
positive++;
}
sum += value;
System.out.print("\nEnter a value: ");
value = input.nextInt(); …Run Code Online (Sandbox Code Playgroud) 如何解决这个声纳问题?
File file = new File("/some directory");
Scanner scanner = new Scanner(file);
Run Code Online (Sandbox Code Playgroud)
删除构造函数“扫描仪(文件)”的这种使用