import java.util.Scanner;
import java.util.*;
public class MultipicationTable{
public static void main(String[] args)
{
// Initialising selection variable to 0
int selection = 0;
int MultiValue = 0;
int UserValue = 0;
// Initializing the count for loop
int i;
// Initializing Random1 and Random2 to get random values
int Random1;
int Random2;
// Creating new Scanner
Scanner input = new Scanner(System.in);
do{
// Menu to select type of multipication
System.out.println("Please select your option");
System.out.println("1: Random Multipication table");
System.out.println("2: Give your …Run Code Online (Sandbox Code Playgroud) 我正在做一个简单的抛硬币游戏,我写了几种方法来调用并使我的主要课程简短.游戏播放一次后,要求用户输入的第一个If/Else语句跳转到Else语句而不提示输入.
package cointoss;
import java.util.Random;
import java.util.Scanner;
public class Game {
int money;
int result;
int bet;
Random rn = new Random();
Scanner in = new Scanner(System.in);
String playerPick;
String aResult;
public void setMoney(int a)
{
money = a;
}
public int getMoney()
{
return money;
}
public void getBet()
{
System.out.println("How much would you like to bet?");
bet = in.nextInt();
do{
if(bet > money)
{
System.out.println("You cannot bet more than you have!");
System.out.println("You have bet " + (bet - …Run Code Online (Sandbox Code Playgroud) 我目前正在制作一个程序,要求输入两个团队的名称和分数.当我请求输入名称和第一组的9分时,扫描仪接受输入就好了.但是,在for循环之后,扫描程序不接受第二个团队名称的输入.这不是整个程序,但我已经包含了所有代码,直到它给我带来麻烦.我怀疑它可能与for循环有关,因为当我将它放在for循环之前,team2接受用户输入就好了.
import java.util.Scanner;
public class sportsGame{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String team1;
String team2;
int team1Scores[] = new int[9]
int team1Total = 0;
int team2Scores[] = new int[9];
int team2Total = 0;
System.out.print("Pick a name for the first team: ");
team1 = input.nextLine();
System.out.print("Enter a score for each of the 9 innings for the "
+ team1 + " separated by spaces: ");
for(int i = 0; i < team1Scores.length; i++){
team1Scores[i] = input.nextInt(); …Run Code Online (Sandbox Code Playgroud) 为什么sc.nextLine();丈夫的空虚如此必要?它并不等于任何东西,而且妻子肯定没有那条线......但如果没有它,程序将无法运行.我的书将其评为Skip over尾随newLine角色.但我不知道有什么可以跳过的!
public class FileTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("For your blended family, \n enter the wife's name");
String wife = sc.nextLine();
System.out.print("Enter the number of her children:");
int herkids = sc.nextInt();
System.out.print("Enter the husband's name:");
//sc.nextLine(); // Why is this line so necessary?
String husband = sc.nextLine();
System.out.print("Enter the number of his children:");
int hisKids = sc.nextInt();
System.out.println(wife + " and " + husband + " have …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试同时接受两个数组的输入.原因是阵列的每个位置处的数据是对应的,例如.姓名和身份证号码.
String[] arr = new String[5];
int[] arr1 = new int[5];
Scanner kb = new Scanner(System.in);
for(int i = 0; i<5; i++)
{
System.out.println("Enter a name:");
arr[i] = kb.nextLine();
System.out.println("Enter an ID:");
arr1[i] = kb.nextInt();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我有这个代码,但每当我运行它时,它会询问一个名称和ID,然后要求两者,但只接受ID.
我似乎无法弄清楚为什么它不允许输入名称,它只是返回一个不兼容的数据类型错误.
System.out.print("Name : ");
String name = in.nextLine();
System.out.print("Age : ");
int age = in.nextInt();
System.out.print("City : ");
String city = in.nextLine();
Run Code Online (Sandbox Code Playgroud)
输出将是:
名称:测试
年龄:20岁
建立成功
当我调试它们时,它不会读取"city"的用户输入.但是当我将"age"的数据类型更改为string时,它将会读取.如何通过系统读取城市的用户输入,将年龄的数据类型保持为int?
我正在实现一个简单的HashMap程序,它存储人名和年龄.这是我的代码:
import java.util.*;
class StoreName {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
String name = sc.nextLine();
int age = sc.nextInt();
map.put(name, age);
}
for (String key : map.keySet())
System.out.println(key + "=" + map.get(key));
}
}
Run Code Online (Sandbox Code Playgroud)
当我从nextInt()获取输入时,Scanner会抛出InputMismatchException异常,但如果我从nextLine()获取输入然后将其解析为int,那么我的代码就会正常运行.请解释一下.
如果我可以将字符串输入解析为任何类型,为什么我应该使用nextInt()或nextDouble().
所以这是我使用的代码:
System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
Run Code Online (Sandbox Code Playgroud)
但是当它到达这一点时,它只是说"创建一个名字".和"创建密码".两个在同一时间然后我必须输入一些东西.所以它基本上是跳过我需要键入字符串的Scanner部分.在"创建一个名字"之后.和"创建密码".是打印出来的,然后输入,名称和密码都改为我输入的内容.如何解决这个问题?
这是全班.我只是测试所以它实际上不是一个程序:
package just.testing;
import java.util.Scanner;
public class TestingJava
{
static int age;
static String name;
static String password;
static boolean makeid = true;
static boolean id = true;
public static void main(String[] args){
makeid(null);
if(makeid == true){
System.out.println("Yay.");
}else{
}
}
public static void makeid(String[] args){
System.out.println("Create your account.");
Scanner input = new Scanner(System.in);
System.out.println("What is your age?");
int age = input.nextInt();
if(age<12){
System.out.println("You are too …Run Code Online (Sandbox Code Playgroud) 我必须为我的课程编写一个程序,该程序将用户输入作为家庭作业,实验室,测试等,将它们放在一个字符串中,并且有一个单独的类将它们分成单独的"双"数字并包含一个writeToFile方法来编写学生的成绩为档案.
我一直遇到问题.当我第一次询问用户他们的名字时,它工作正常,但如果用户决定再次进入do-while循环,则会跳过"你的名字是什么",它会消失直接到id.我知道它与最后一个不是字符串的输入有关,而解决方案就是放一个"keyboard.nextLine();" 在问题之上,但我尝试过,它甚至在询问问题之前只询问用户他们的名字..
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class GradeApplication
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
//Define variables
String name;
int id;
String homework;
String labs;
String tests;
double project;
double discussion;
int answer;
do
{
System.out.print("\nWhat is your name? ");
name=keyboard.nextLine();
System.out.print("\nWhat is your student ID? ");
id=keyboard.nextInt();
homework = keyboard.nextLine();
System.out.println("\nPlease enter homework grades separated by spaces:");
homework = keyboard.nextLine();
System.out.println("Please enter lab grades separated by spaces:");
labs …Run Code Online (Sandbox Code Playgroud) 嗨,我正在练习java,只学习java几天.我需要减去500000的奖学金金额100000,但我似乎被困在400000并且它不会继续.我的代码有什么问题请有人帮助我.提前致谢.
import java.util.Scanner;
import java.util.LinkedList;
public class Main
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
Application a = new Application();
LinkedList lList = new LinkedList();
boolean bEligible = false;
int scholarship = 100000;
int scholarshipAmount = 500000;
int amount;
while(scholarshipAmount != 0)
{
System.out.println("Please enter name");
a.setsName(input.nextLine());
System.out.println("1–Pre Diploma, 2–Diploma, 3–Degree");
System.out.println("Please enter your Study Level");
a.setiStudyLevel(input.nextInt());
System.out.println("Please enter your Personality Score");
a.setiPersonalityScore(input.nextInt());
System.out.println("Please enter your Parents Incomee");
a.setdParentsIncome(input.nextDouble());
String sName = a.getsName();
int …Run Code Online (Sandbox Code Playgroud) 我需要解决一个问题,当输入整数时,这是用户想要在此输入旁边输入的行数(一些句子),如文本所述,如下所示:
第一行输入包含一个整数N,表示输入中的行数.接下来是N行输入文本.
我写了以下代码:
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String lines[] = new String[n];
for(int i = 0; i < n; i++){
System.out.println("Enter " + i + "th line");
lines[i] = scan.nextLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
并与该计划的互动:
5(The user inputted 5)
Enter 0th line(Program outputted this)
Enter 1th line(Doesn't gave time to input and instantly printed this message)
Hello(Gave time to write some input)
Enter 2th line(Program outputted this)
How(User input)
Enter …Run Code Online (Sandbox Code Playgroud)