您好,我试图编写一个测试运算符的代码,需要一些帮助来声明变量.因此,在我的if语句下的代码中,我声明某个答案是真或假,这取决于输入的数字是什么,我一直卡在"找不到符号"错误上.这是我的代码
import java.util.Scanner;
public class TestOperators
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//prompt the user to enter an integer
System.out.print("Enter an integer: ");
int num = input.nextInt();
String t = new String("True");
String f = new String("False");
//calculate
if ((num % 5 == 0) && (num % 6 == 0))
answer = t;
else if ((num % 5 == 0) && (num % 6 != 0) || (num % 5 != 0) && (num …Run Code Online (Sandbox Code Playgroud) 我想询问用户是否要创建一个名为file.elt或不是的文件.我正在尝试使用Scannerclass 使用switch语句.
这是我的代码:
System.out.println("Do you want to create the file.elt? (Y/N)");
strOption=sc.nextLine();
OUTER:
while (sc.hasNext()) {
switch (strOption) {
case "Y":
case "y":
elements.createElements(file);
break OUTER;
case "N":
case "n":
System.out.println("There will be no file.elt created! .");
break OUTER;
default:
System.out.println("Please, type Y or N.");
strOption=sc.nextLine();
break;
}
}
sc.close();
Run Code Online (Sandbox Code Playgroud)
该sc对象在程序开头声明,我要求提供文件名.
该sc声明是:
String file;
Scanner sc = new Scanner(System.in);
System.out.println("Type the name of the file .dat .");
file=sc.nextLine();
Run Code Online (Sandbox Code Playgroud)
问题是while循环是无限的,我不知道为什么.
我正在制作一个纯粹用于学习目的的基本计算器.这是有问题的代码块:
Scanner Operation = new Scanner(System.in);
Scanner data = new Scanner(System.in);
String add, sub, mul, div;
double fnum, snum, answer;
add = Operation.nextLine();
System.out.println(add);
if (Operation.equals("add")) {
System.out.println("Enter the first number: ");
fnum = data.nextDouble();
System.out.println("Enter second number: ");
snum = data.nextDouble();
answer = fnum + snum;
System.out.println("Your answer is " + answer);
}
Run Code Online (Sandbox Code Playgroud)
程序要求输入和终止两个而不运行if语句.
任何帮助将非常感激.
我编译没有错误,但是我可以完成第一个循环没有问题.然而,第二次绕过它将提示分区名称但是员工数量的提示确实如此.所以我最终看到输入部门名称:输入员工人数:
不知道为什么会发生这种情况,但是在看的时候我看不到任何不合适的地方.
如果您看到我出错的地方,您可以指向行号或代码串.
谢谢.
import java.util.Scanner;
public class PayRoll {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int employees;
double salary;
while(true) {
System.out.print("Enter Division Name: ");
name = input.nextLine();
if(name.equalsIgnoreCase("stop")) {
break;
}else {
System.out.print("Enter Number of Employees: ");
employees = input.nextInt();
while(employees <= 0) {
System.out.print("Number of Employees Must be Greater than 0, Please Re-enter: ");
employees = input.nextInt();
}
System.out.print("Enter Average Salary: ");
salary = input.nextDouble();
while(salary <= 0) {
System.out.print("Average …Run Code Online (Sandbox Code Playgroud) 全部!我在扫描仪/传递文本文件作为方法的参数时遇到问题。如果我在 main 中创建一个 Scanner,我就可以做所有事情。
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class App {
public static void main(String[] args){
File file = new File([FILEPATH]);
Scanner inputFile = new Scanner(file);
...
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将文本文件传递给方法并在那里创建扫描仪时,我收到 FileNotFoundException
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class App {
public static void main(String[] args){
File file = new File([FILEPATH]);
method(file);
}
public static String method(File file){
Scanner inputFile = new Scanner(file); //This is where I get the error :(
...
Run Code Online (Sandbox Code Playgroud)
问题的提示特意说参数必须是文件,所以我不能改成String参数来传递文件路径。
我使用 .getPath() 来确保我的方法中文件的文件路径与 main 中文件的文件路径匹配,而且确实如此。我不确定需要修复什么。非常感谢任何帮助!
编辑; …
我们必须在java中创建一个类似于测验的程序,但是我的else语句对于给出的每个答案都说得对.这就是我所拥有的.
import java.util.Scanner;
public class Project03 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your name:");
String name = keyboard.nextLine();
System.out.println("Welcome:" + name );
System.out.println("Please answer the following questions:\n" + "What is 4 + 6 \n");
keyboard.nextLine();
int x = 10;
if (x == 10){
System.out.println("Correct!");
}else{
System.out.println("Incorrect!");
}
}
}
Run Code Online (Sandbox Code Playgroud)