gpt4 book ai didi

java - 扫描仪在给出最终输出之前请求多个输入

转载 作者:行者123 更新时间:2023-12-02 03:54:15 25 4
gpt4 key购买 nike

所以我是 Java 新手,正在尝试将变量从扫描仪传递到我的 getter/setter 方法,以便根据输入的数字对数组进行不同的排序。

我已经知道了列表的排序位置;问题是我的扫描仪会重复,您必须多次输入您的选择才能显示列表。

我知道问题与调用“int c = allocate_6.choice()”有关。如果我硬编码一个数字,那没问题,但它似乎对 choice() 函数进行了多次调用。

我尝试将函数移出主函数,并删除 setter/getter 文件中的 Comparable,还删除快速排序并使用数组和集合。这些都不起作用。
我觉得这可能是我犯的一个愚蠢的错误,并且由于不太了解 Java 而错过了它。可以使用帮助来解决这个问题。

这是我的输出:

nter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
Enter number 1,2 or 3: 1
contacts [age=20, state=Alabama, firstname=Pickles, lastname=Cattle]
contacts [age=35, state=New York, firstname=George, lastname=Constanza]
contacts [age=90, state=Florida, firstname=Adam, lastname=Tree]
contacts [age=32, state=Illinois, firstname=Mary, lastname=Upton]
contacts [age=58, state=Washington, firstname=Bob, lastname=Wiseman]

代码:

    import java.util.Scanner;

public class test_6 {

public static void main(String[] args) {


Contacts[] a = {
new Contacts(32, "Illinois", "Mary", "Upton"),
new Contacts(58, "Washington", "Bob", "Wiseman"),
new Contacts(20, "Alabama", "Pickles", "Cattle"),
new Contacts(35, "New York", "George", "Constanza"),
new Contacts(90, "Florida", "Adam", "Tree"),
};

Quick.sort(a);
for (Contacts contacts:a){
System.out.println(contacts);
}
}

public static int choice() {
System.out.print("Enter number 1,2 or 3: ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
}

public class Contacts implements Comparable<Contacts>{

Integer age;
String state;
String firstname;
String lastname;
int c = test_6.choice();

public Contacts(Integer age, String state, String firstname, String lastname){
this.age = age;
this.state = state;
this.firstname = firstname;
this.lastname = lastname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int compareTo(Contacts contacts) {

if (c == 1){
return this.getLastname().compareTo(contacts.getLastname());
}
else if (c == 2){
return this.getState().compareTo(contacts.getState());
}
else if (c == 3){
return this.getAge().compareTo(contacts.getAge());
}
else return 0;

}
@Override
public String toString() {
return "contacts [age=" + age + ", state=" + state + ", firstname=" + firstname + ", lastname=" + lastname
+ "]";
}
}

最佳答案

我不完全确定,您可以调试,但这可能与在全局范围内分配您的函数有关。您最好将静态函数 choice() 移动到您的 Contacts 类/对象中。

然后从初始化器 Contacts 内部调用 choice() 函数。例如:

import java.util.Scanner;

public class test_6 {

public static void main(String[] args) {


Contacts[] a = {
new Contacts(32, "Illinois", "Mary", "Upton"),
new Contacts(58, "Washington", "Bob", "Wiseman"),
new Contacts(20, "Alabama", "Pickles", "Cattle"),
new Contacts(35, "New York", "George", "Constanza"),
new Contacts(90, "Florida", "Adam", "Tree"),
};

Quick.sort(a);
for (Contacts contacts:a){
System.out.println(contacts);
}
}

//Function Choice Moved to Contacts.choice()
}

public class Contacts implements Comparable<Contacts>{

Integer age;
String state;
String firstname;
String lastname;
int c;

public Contacts(Integer age, String state, String firstname, String lastname){
this.age = age;
this.state = state;
this.firstname = firstname;
this.lastname = lastname;
this.c = choice();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int compareTo(Contacts contacts) {

if (c == 1){
return this.getLastname().compareTo(contacts.getLastname());
}
else if (c == 2){
return this.getState().compareTo(contacts.getState());
}
else if (c == 3){
return this.getAge().compareTo(contacts.getAge());
}
else return 0;

}
public static int choice() {
System.out.print("Enter number 1,2 or 3: ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}
@Override
public String toString() {
return "contacts [age=" + age + ", state=" + state + ", firstname=" + firstname + ", lastname=" + lastname
+ "]";
}
}

关于java - 扫描仪在给出最终输出之前请求多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com