当前位置: 首页 > 知识库问答 >
问题:

用户输入问题

姚雅珺
2023-03-14

我需要一种允许用户输入团队名称和团队成员名称的方法。我试过使用扫描仪,但由于我对Java的经验不足,它无法正常工作。我只想让一个用户输入一个团队名称和四个团队成员的名字。我将这些变量公之于众,但我不知道如何向它们输入数据,因为它们在单独的类中。

这是密码

main类导入java.util.Scanner;公共类Main{

public static void main(String[] args) {
    Competition comp1 = new Competition("First competition", "IUS team1", "IUS team2" ,2014 );
    Competition comp2 = new Competition("Second competition" , "IUS team3", "IUS team4", 2016);
    Scanner jin = new Scanner(System.in);
    Team teamObject1 = new Team(String Team_name);
    String Team_name = teamObject1.jin.nextLine();

    //Team team1 = new Team("IUS team1", "Adin", "Merzuk", "Segmedina", "Iman", comp1, comp2); 
    Team team2 = new Team(team1); 


    System.out.println(team1);


}

   }
   public class Team {

public String Team_name; 
public String name1; 
public String name2; 
public String name3; 
public String name4; 
private Competition competition1; 
private Competition competition2; 

public Team(String Team_name, String name1, String name2, String name3, String name4, Competition comp1, Competition comp2){

    this.Team_name = Team_name; 
    this.name1 = name1; 
    this.name2 = name2; 
    this.name3 = name3; 
    this.name3 = name3; 
    this.name4 = name4; 
    competition1 = new Competition(comp1); 
    competition2 = new Competition(comp2); 

    }

    public Team(Team original){
        Team_name = original.Team_name;
        name1 = original.name1; 
        name2 = original.name2; 
        name3 = original.name3; 
        name4 = original.name4; 
        competition1 = original.competition1;
        competition2 = original.competition2;
    }

    public void setTeam_name(String Team_name){ 
        this.Team_name = Team_name; 
    }

    public void setNames(String name1, String name2, String name3, String name4){
        this.name1 = name1; 
        this.name2 = name2; 
        this.name3 = name3; 
        this.name4 = name4;
    }

    public String getTeam_name(){ 
        return Team_name; 
    }
    public String getName1(){ 
        return name1; 
    }
    public String getName2(){ 
        return name2; 
    }
    public String getName3(){ 
        return name3; 
    }
    public String getName4(){ 
        return name4; 
    }

    public String toString(){ 
        return("Team name: " + getTeam_name() + "\nMember1: " + getName1() +"\nMember2: " + getName2() + "\nMember3; "  + getName3()+"\nMember4: " + getName4() +"\n" +  competition1+ "\n"+ competition2  );
    }
   }
   public class Competition {

private String nameCompetition; 
private String Winning_team; 
private String runner_up; 

private int Year_Competition; 


public Competition(){ 
    nameCompetition = "Not set"; 
    Winning_team = "Not set"; 
    runner_up = "Not set"; 
    Year_Competition = 0; 
}

public Competition(String nameCompetition, String Winnig_team, String runner_up, int Year_Competition ){

    this.nameCompetition = nameCompetition; 
    this.Winning_team = Winnig_team; 
    this.runner_up = runner_up;
    this.Year_Competition = Year_Competition;

}

public Competition(Competition comp1){ 
    nameCompetition = comp1.nameCompetition;
    Winning_team = comp1.Winning_team; 
    runner_up = comp1.runner_up;
    Year_Competition = comp1.Year_Competition;
}

public void setNameCompetition(String nameCompetition){ 
    this.nameCompetition = nameCompetition;
}

public void setWinningTeam(String Winning_team){ 
    this.Winning_team = Winning_team; 
}

public void setRunnerUp(String runner_up){ 
    this.runner_up = runner_up; 
}

public void setYear(int year_competition){ 
    this.Year_Competition = year_competition; 
}

public String getNameCompetition(){ 
    return nameCompetition; 
}

public String getWinningTeam(){ 
    return Winning_team; 
}
public String getRunnerUp(){ 
    return runner_up; 

}
public int getYear(){ 
    return Year_Competition; 
}

public String toString(){ 
    return("Competition name: " + getNameCompetition() + " Winners: " + getWinningTeam() + " runner up: " + getRunnerUp() + " Year: " + getYear());
}



   }

共有1个答案

穆正青
2023-03-14
Scanner scanner = new Scanner(System.in);

System.out.print("Team name: ");
String teamName = scanner.nextLine();
System.out.print("Player 1: ");
String player1 = scanner.nextLine();
System.out.print("Player 2: ");
String player2 = scanner.nextLine();
System.out.print("Player 3: ");
String player3 = scanner.nextLine();
System.out.print("Player 4: ");
String player4 = scanner.nextLine();

Team team = new Team(teamName, player1, player2, player3, player4, comp1, comp2);
 类似资料:
  • 问题内容: 我有一个班级任务要使用扫描仪读取数据。 最下面的部分是对我的测试,以检查是否一切正常。但是,虽然我已设置了while循环,但似乎不想继续运行一次以上。它会取一个值,然后如果我说是,我有更多的值(y或Y),它将退出并打印两个笨蛋 问题答案: scan.nextLine()时,它将读取不等于Y或y的结束符。结果,它退出了while循环。像这样更正您的代码,只需更改您正在阅读doubel的一

  • 我有一个课堂作业,用扫描器读取数据。 底部的部分只是为了测试我是否所有的信号都正常运行。但是,我安装的while循环似乎不想继续运行多次。它将接受一个值,然后如果我说是,我有更多的值(y或y),它将退出并打印任何一个胸部

  • 在Angular 2中,您可以使用HTML的DOM元素结构来在运行时更改元素的值。 我们来看一些细节。 输入标签 在app.component.ts文件中放置以下代码。 import { Component } from '@angular/core'; @Component ({ selector: 'my-app', template: ' <div

  • 我希望“请输入一个带有字母S的句子”循环,直到用户输入字母“S”

  • 问题内容: 我想知道如何编写一个可以接受多行输入的简单程序,然后可以像在lynx浏览器中一样提交输入,在该浏览器中您可以使用空白行然后使用句点来提交输入。 我想在电子邮件程序中使用它。 问题答案: 这是一个简单的方法:

  • 我正在开发一个待办事项列表程序。 我想让用户可以输入日期、时间和关于。我已经知道日期了。我错过的是时间。 但我有很多例外。我错过了什么吗?