![](/img/trans.png)
[英]Getting error File cannot be resolved to a type for java in visual studio code
[英]I keep getting an "Scanner cannot be resolved to a type" error. I am using Java and Visual Studio and this is for a beginner Java class
这是针对初学者的 Java 课程。 我不断收到“扫描仪无法解析为类型”错误。 我正在使用 Visual Studio 和 JDK 16.0.2 我也刚刚安装了 Visual Studio 并在这台笔记本电脑上安装了 Java。 我正在为我的班级编写这个 Mortgage Calculator 程序,它在我添加用于捕获用户输入错误的 while 循环之前就起作用了。 现在它给了我以前没有的“扫描仪”错误。 我添加并移动了几行代码,突然间它给了我这个“扫描仪”错误。 下面是整个代码:
package com.phillip;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
int principal = 0;
float monthlyInterest = 0;
int numberOfPayments = 0;
Scanner scanner = new Scannner(System.in);
while (true) {
System.out.println("Principal: ");
principal = scanner.nextInt();
if (principal >= 1000 && principal <= 1_000_000) {
break;
}
System.out.println("Enter a value between 1000 and 1000000");
}
while (true) {
System.out.println("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
if (annualInterest >= 1 && annualInterest <= 30) {
monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 and 30");
}
while (true) {
System.out.println("Period (Years): ");
byte years = scanner.nextByte();
if (years >= 1 && years <= 30) {
numberOfPayments = years * MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a value between 1 & 30");
}
double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("Mortgage: " + mortgageFormatted);
}
}
我相信你有一个错字
Scanner scanner = new Scannner(System.in);
“扫描仪”而不是“扫描仪”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.