java - 如何修复错误预期的?
问题描述
代码返回错误<identifier> expected
。我不知道为什么会发生这个错误。
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
intro();
time();
}
public static void intro() {
System.out.println("Welcome");
System.out.println("What is your name");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
System.out.println("Nice to meet you," + name + " where are you travelling to?");
String dest = input.nextLine();
System.out.println("Great!" + dest + " sounds like a great trip");
}
public static void time() {
Scanner input = new Scanner(System.in);
int hours, minutes;
float perd, perdc, change;
System.out.println("How many days are you going to spend travelling?");
int days = input.nextInt();
hours = days * 24;
minutes = hours * 60;
System.out.println("How much money in USD are you going to spend?");
Double money = input.nextDouble();
perd = (money / days);
System.out.println("What is the three letter currency symbol of your destination?");
String curr = input.nextLine();
System.out.println("How many" + curr + "are there in 1USD?");
Double ex = input.double();
change = money * ex;
perdc = perd * ex;
System.out.println("If you are travelling for" + days + "that is the same as" + hours + "or" + minutes + "minutes");
System.out.println("If you are going to spend" + money + "$USD that means per day you can spend upto $" + perd + "USD");
System.out.println("Your total budget in" + ex + "is" + change + ex + ",which per day is " + perdc + curr);
}
}
解决方案
我很高兴看到人们学习 Java,也很高兴看到人们帮助小辈。当我看到您的代码时,我的第一个建议是获得一个好的 IDE,例如 Eclipse、IntelliJ 或 Netbeans,它将帮助您快速查看编译错误。
我的第二个建议是查看开发规范,作为一名初级人员,我认为这是您必须学习的第一种方法,以拥有全面且可维护的代码。
例如,请避免在方法签名和第一个语句之间添加几个空行。我几乎没有碰过你的代码来让它工作。我希望你会喜欢 Java。
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
intro(input);
time(input);
}
public static void intro(Scanner input) {
System.out.println("Welcome");
System.out.println("What is your name");
String name = input.nextLine();
System.out.println("Nice to meet you, " + name + " where are you travelling to?");
String dest = input.nextLine();
System.out.println("Great! " + dest + " sounds like a great trip");
}
public static void time(Scanner input) {
int hours, minutes;
float perd, perdc, change;
System.out.println("How many days are you going to spend travelling?");
int days = input.nextInt();
hours = days * 24;
minutes = hours * 60;
System.out.println("How much money in USD are you going to spend?");
Float money = input.nextFloat();
perd = money / days;
System.out.println("What is the three letter currency symbol of your destination?");
String curr = input.nextLine();
System.out.println("How many " + curr + " are there in 1USD?");
Float ex = input.nextFloat();
change = money * ex;
perdc = perd * ex;
System.out.println("If you are travelling for " + days + " that is the same as " + hours + " or " + minutes + " minutes");
System.out.println("If you are going to spend " + money + " $USD that means per day you can spend upto $" + perd + " USD");
System.out.println("Your total budget in " + ex + " is " + change + ex + " ,which per day is " + perdc + curr);
}
推荐阅读
- roomle - 如何创建自定义 AR 按钮
- python - 在数据框中,从起始元素中查找小于/大于 0.05 的元素。然后将这个新索引作为第一个迭代器
- elasticsearch - ElasticSearch 查询错误:查询格式错误,没有 start_object
- arrays - 查找唯一 ID,将行复制并粘贴到新选项卡,如果 ID 重复,则将某些行合并在一起
- javascript - 无法读取 null 的属性“帖子”
- django - 多个 Django 子表单在主表单中使用 ajax 显示,但提交子表单时未注册数据(无错误)
- python - 如何计算子目录?
- c - LONG_MAX -1 在 64 位机器上?
- java - 如何在 talend 的文件夹中查找 .TXT 文件并使用 tftpget?人才
- macos - 在 macos big Sur 11.2.3 中找不到 imageio