读取和存储数组中的唯一值(JAVA)
所以我的java作业有问题。任务是编写一个程序,读取十个数字并仅显示不同的数字以及不同值的数量。到目前为止我所得到的是...
import java.util.Scanner;
import java.util.Collection;
public class Exercise06_05 {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int[] list = new int[10];//create my new 10 slot array
//i really want a variable length array but alas
for (int i = 0; i < 10; i++){//get and check the ten input variables
System.out.println("Enter an Integer");//ask for input
int integer = input.nextInt();//assign the input to a temp variable
if (isUnique(list, integer)){//check if the temp is unique to the array
list[i] = integer;//if so assign it
}
}
String output = "";
int j = 0;
for (j = 0; j < list.length; j++){
if(list[j] != 0){//this is where the error accours
output += (list[j] + " ");
}else
break;//this break ensures J doesn't get any higher
//so that i can plug that in for the number of distinct variables
}
System.out.println("The number of distinct numbers is " + j);
System.out.println(output);//print output, and the number of distinct values
}
public static boolean isUnique(int [] arry, int a){// my masterpiece of a method
for (int i = 0; i < (10);){
if (arry [i] == a){//check box
return false;//not unique
} else if (i == (arry.length - 1)){//we done yet?
return true;//if so, return that it's unique
}else//if we're not done increment the box
i++;//there that is
} return false;//now put this here just to safeguard
}
}
它工作正常,除非用户连续输入两个相同的整数,例如 1,然后再次输入 1。发生的情况是程序不存储第二个 int,数组保留零,然后在创建输出部分失败。我该如何解决这个问题?
So i've got a problem in my java homework. The task is to write a program that reads in ten numbers and displays only distinct numbers along with the number of distinct values. what i've got so far is...
import java.util.Scanner;
import java.util.Collection;
public class Exercise06_05 {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int[] list = new int[10];//create my new 10 slot array
//i really want a variable length array but alas
for (int i = 0; i < 10; i++){//get and check the ten input variables
System.out.println("Enter an Integer");//ask for input
int integer = input.nextInt();//assign the input to a temp variable
if (isUnique(list, integer)){//check if the temp is unique to the array
list[i] = integer;//if so assign it
}
}
String output = "";
int j = 0;
for (j = 0; j < list.length; j++){
if(list[j] != 0){//this is where the error accours
output += (list[j] + " ");
}else
break;//this break ensures J doesn't get any higher
//so that i can plug that in for the number of distinct variables
}
System.out.println("The number of distinct numbers is " + j);
System.out.println(output);//print output, and the number of distinct values
}
public static boolean isUnique(int [] arry, int a){// my masterpiece of a method
for (int i = 0; i < (10);){
if (arry [i] == a){//check box
return false;//not unique
} else if (i == (arry.length - 1)){//we done yet?
return true;//if so, return that it's unique
}else//if we're not done increment the box
i++;//there that is
} return false;//now put this here just to safeguard
}
}
It works fine unless the user inputs two of the same ints in a row like 1 and then 1 again. What happens is the program doesn't store the second int, the array keeps a zero and then fails at the create output part. How do i get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,这有点不同,这不是回答你的问题,你已经这样做了,而是更多地关注你的解决方案。您首先阅读数字列表,然后对这些数字进行操作。
这是另一个片段:
它只读取一次并且只循环一次。
Ok this is little different, it's not about answering your question, you already did that, it's about a bit more focus on your solution. You are reading the list of numbers first and then operating on those numbers.
Here is another snippet:
That's it read only once and loop only once.
好吧,一方面,如果您获得的数字是唯一的,则将其插入数组中。如果它不唯一,则不会将其插入数组,但仍会提前数组索引。因此,您将保留它的初始化默认值为零。
这有两个问题。
无法区分“重复值”和“零”。如果零不是合法值,那好吧。如果零是合法的,那么这将不起作用。
更基本的是,当您在输出上循环遍历数组时,当您遇到第一个零时就会退出。因此,如果用户输入 1,2,4,2,3,4,您将用 1,2,4,0,3,0 填充数组。然后,当您显示输出时,您将写入 1,2,4,看到零并退出,并声明有 3 个唯一值。你永远不会达到 3。
我不知道他们已经教了你多少关于可用数据结构的知识。更好的解决方案是使用 ArrayList 而不是数组,然后仅在传入值唯一时才添加到 ArrayList。如果您还没有了解这一点,另一个想法是在您进行过程中保留唯一值数量的计数器。当获得重复值时,不要插入数组,也不要增加计数器。也就是说,有一个计数器表示数组中的位置,与唯一值的数量相同。还有另一个计数器,它是读取的输入值的数量。一旦您看到第一个重复项,这将大于数组的位置。
在一个细节点上,与您的问题没有直接关系:在您的 isUnique 函数中,为什么要循环到硬编码的 10,然后有一个单独的 IF 语句来测试数组长度并在到达末尾时中断?如果你编码的话,它会更简单、更容易阅读:
或者,正如我上面建议的,你有一个单独的变量来说明数组实际填充了多少:
Well, for one thing, if the number you get is unique you insert it into the array. If it is not unique, you don't insert it into the array, but you still advance the array index. Thus, you're going to leave it with the initialization default value of zero.
There's two problems with this.
There's no way to distinguish "duplicate value" from "zero". If zero is not a legal value, then okay fine. If zero is legal, this won't work.
More fundamental, when you loop through the array on output, you quit when you hit the first zero. So if the user entered, say, 1,2,4,2,3,4, you're goint to fill your array with 1,2,4,0,3,0. Then when you display the output you're going to write 1,2,4, see the zero and quit, and declare that there are 3 unique values. You'll never reach the 3.
I don't know how much they've taught you about the available data structures yet. A better solution would be to use an ArrayList rather than an array, and then only add to the ArrayList when an incoming value is unique. If you haven't learned about that yet, another idea would be to keep a counter of the number of unique values as you go along. Don't insert in the array when you get a duplicate value, and also don't increment the counter. That is, have one counter that's the position in the arrray, which is the same as the number of unique values. Have another counter which is the number of input values read. This will be greater than the position of the array once you see the first duplicate.
On, on one detail point, not directly related to your question: In your isUnique function, why do you loop up to a hard-coded ten, and then have a separate IF statement to test the array length and break when you reach the end? It would be a lot simpler and easier to read if you coded:
Or if, as I suggest above, you have a separate variable to say how much of the array is actually filled:
您的问题是您试图输出比数组中更多的值。
执行
int[] list = new int[10]
意味着 list.length 将始终为 10。要么从数组切换到列表,要么跟踪插入的数字。
Your problem is that you're trying to output more values than are in the array.
Doing
int[] list = new int[10]
means that list.length will always be 10.Either switch from an array to a List, or keep track of the number inserted.
即使没有存储该值,i也会增加1。
一种解决方案是:
将0存储在单元格中,否则单元格中什么都没有(甚至没有0),程序崩溃。
相反,如果您想为单元格获取另一个值,请将 i 减一,而不是为单元格分配 0。
Even if the value not stored, i is increased by 1.
One solution is:
Which will store 0 in the cell, otherwise there is nothing (not even 0) in the cell, and the program crashes.
Instead, if you want to get another value to the cell, decrease i by one instead of assigning 0 to the cell.