【发布时间】:2015-09-21 04:10:01
【问题描述】:
我是编码初学者,我正在尝试创建一个简单的程序,当读者输入他们的名字时,程序会显示他们欠多少钱。我正在考虑使用Scanner、next(String) 和int。
import java.util.Scanner;
public class moneyLender {
//This program will ask for reader input of their name and then will output how much
//they owe me. (The amount they owe is already in the database)
public static void main(String[] args) {
int John = 5; // John owes me 5 dollars
int Kyle = 7; // Kyle owes me 7 dollars
//Asking for reader input of their name
Scanner reader = new Scanner(System.in);
System.out.print("Please enter in your first name:");
String name = reader.next();
//my goal is to have the same effect as System.out.println("You owe me " + John);
System.out.println("You owe me: " + name) // but not John as a string but John
// as the integer 5
//Basically, i want to use a string to call an integer variable with
//the same value as the string.
}
}
【问题讨论】:
-
创建一个
映射并根据名称(作为键)检索 int(值)。
标签: java int java.util.scanner