我需要询问用户5个数字,只有50-100有效。我需要检查它,看看数字是否是唯一的。我有一切工作,除了独特的方法。我一直在网上搜索,我尝试了很多方法让这个方法起作用。我的大脑不和我合作。我添加了一个新功能。
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;
public class Valid {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int[] numbers;
int uniqueCount = 0;
int testInput;
numbers = new int [5];
// go up from 0 to 4 check if isValid if not take away 1 step from i.
for (int i = 0; i < numbers.length; i++){
System.out.print("Enter an integer (50 - 100): ");
if (isValid(testInput = input.nextInt()) == true){
numbers[i] = testInput;
System.out.println(isUnique(numbers,i));
/*if (isUnique(numbers) == true){
uniqueCount+=1;
System.out.println("Unique so far: " + uniqueCount);
} else {
System.out.println("Unique so far: ");
System.out.print(uniqueCount);
}*/
}else {
i--;
}
}
}
static boolean isValid(int n){
boolean valid = true;
// Simple if under 50 or over 100 it is not Valid
if (n < 50 || n > 100){
System.out.println("Error entered non valid number must be 50-100.");
valid = false;
} else valid = true;
return valid ;
}
static boolean isUnique(int[] nArray){
for ( int i =0; i < nArray.length - 1; i++){
for (int j =i + i ; j < nArray.length; j++){
if (nArray[i] == nArray[j]){
return false;
}
}
}
return true;
}
}
static boolean isUnique(int[] nArray, int currentSizeOfArray){
if (currentSizeOfArray == 0){
//only number got to be unique
return true;
}else {
for (int i = 0; nArray.length < currentSizeOfArray;i++){
if (nArray[i] == nArray[0] || nArray[i] == nArray[1] || nArray[i] == nArray[2] || nArray[i] == nArray[3] || nArray[i] == nArray[4] ){
// not unique
return false;
}
}
}
return true;
}
答案 0 :(得分:0)
将第二个循环中j
的初始化从j = i + i
更改为j = i + 1
;)
<强>更新强> 您在下面的回答中写道:
if (currentSizeOfArray == 0){
//only number got to be unique
return true;
} else if (currentSizeOfArray == 1){
if (nArray[1] == nArray[0]){
return false;
}
...
你解决了这个问题。它可以用更优雅的方式编写:
static boolean isUnique(int[] nArray, int currentSizeOfArray){
for(int i=0; i < currentSizeOfArray; i++) {
for (int j=0; j < i; j++) {
if (nArray[j] == nArray[i]) {
return false;
}
}
}
return true;
}
答案 1 :(得分:0)
static boolean isUnique(int[] nArray, int currentSizeOfArray){
if (currentSizeOfArray == 0){
//only number got to be unique
return true;
} else if (currentSizeOfArray == 1){
if (nArray[1] == nArray[0]){
return false;
}
}else if (currentSizeOfArray == 2){
if (nArray[2] == nArray[0] || nArray[2] == nArray[1]){
return false;
}
}else if (currentSizeOfArray == 3){
if (nArray[3] == nArray[0] || nArray[3] == nArray[1]|| nArray[3] == nArray[2]){
return false;
}
}else if (currentSizeOfArray == 4){
if (nArray[4] == nArray[0] || nArray[4] == nArray[1]|| nArray[4] == nArray[2] || nArray[4] == nArray[3]){
return false;
}
}
return true;
}
这解决了它,但我完全不喜欢它。
答案 2 :(得分:0)
static boolean isUnique(int[] nArray){
boolean unique = true;
try{
Set<Integer> set = new HashSet<>();
for(int value : nArray){
set.add(value);
}
}catch (IllegalArgumentException ignore){
unique = false;
}
return unique;
}
答案 3 :(得分:0)
最好你可以检查当前数组中是否已存在新输入的整数。如果你还想要一个方法:
static boolean isUnique(int[] array, int length, int newValue){
for(int i=0;i< length;i++){
if(array[i] == newValue){
return false;
}
}
return true;
}
完成此检查后,您可以将newValue
添加到数组中。
答案 4 :(得分:0)
顺便说一句,您的示例代码完全不是面向对象的,没有支持类,没有使用集合,以及所有静态代码。看起来更像是C或BASIC代码而不是Java。
为了让您脱离main
方法的静态上下文,我定义并实例化了一个App
类。 main
方法不是面向对象的。从概念上讲,我忽略了main
方法,因为这只是启动应用程序chicken-or-the-egg question的解决方案。
在Java Collections框架(Tutorial)中,Set是Collection,不能包含重复元素。 SortedSet是一个以排序顺序维护其元素的Set。我使用它是为了方便按顺序查看我们的数字。
如果您需要跟踪用户数据输入的顺序,请使用List。它可能包含重复项。因此,只有在项目中未包含项目时才添加项目。
顺便说一句,上面使用的“distinct”,“set”和“list”是编程和Java中的技术术语。在本课题中,“distinct”一词比“unique”更合适。
正如本Answer所述,在处理不可靠的数据时(人类非常不可靠),调用nextLine
比nextInt
更有意义。这也允许您添加“退出”命令,以便用户可以优雅地从应用程序的离合器中逃脱。在检查该行是否包含诸如“exit”之类的关键词之后,将其解析为Integer对象以存储在Set中(可能还有List)。
package com.example.score;
import java.time.Instant;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
* @author Basil Bourque
*/
public class App {
// Constructor
public App () {
super();
// Do initialization here. We have no need currently.
}
private void doIt () {
int limit = 5;
SortedSet<Integer> integers = new TreeSet<>();
System.out.print( "Enter an integer from 50 to 100 inclusive (50 - 100): " );
try (
// Using try-with-resources syntax to automatically close the Scanner.
Scanner input = new Scanner( System.in ); ) {
Boolean go = Boolean.TRUE;
Integer integer = null;
while ( ( integers.size() < limit ) && go ) { // While we need more numbers AND the user wants to continue.
String line = input.nextLine(); // Gather input from user.
if ( line.equalsIgnoreCase( "exit" ) ) { // Check to see if the user wants to end our program early.
System.out.println( "You chose to exit this program before completing data-entry of " + limit + " numbers. " + Instant.now() );
go = Boolean.FALSE;
continue;
}
try {
int i = Integer.parseInt( line );
if ( ( i >= 50 ) && ( i <= 100 ) ) { // Verify the value input is within expected range.
integer = i; // Auto-boxing handles converting from a primitive int to an object Integer.
} else {
System.out.println( "Bad… Your entry : ‘" + line + " is not in the expected range. Please enter an integer from 50 to 100 inclusive. " + integers.size() + " of " + limit + " entered so far. To end this program, type 'exit'." );
continue;
}
} catch ( NumberFormatException e ) {
System.out.println( "Bad… Your entry is not an integer : ‘" + line + "’ at " + Instant.now() + ". Please enter an integer from 50 to 100 inclusive. " + integers.size() + " of " + limit + " entered so far. To end this program, type 'exit'." );
continue;
}
// Should have a valid Integer object in hand at this point. Test if duplicate.
{
if ( integers.contains( integer ) ) {
System.out.println( "Bad… Duplicate entry ‘" + integer + "’ at " + Instant.now() + ". Please enter an integer from 50 to 100 inclusive. " + integers.size() + " of " + limit + " entered so far. To end this program, type 'exit'." );
} else {
integers.add( integer );
System.out.println( "Good… You entered ‘" + integer + "’ at " + Instant.now() + ". Please enter another integer from 50 to 100 inclusive. " + integers.size() + " of " + limit + " entered so far. To end this program, type 'exit'." );
}
}
}
}
System.out.println(
"Data entry complete at " + Instant.now() + "." );
System.out.println(
"Numbers entered, in sorted order: " );
for ( Integer integer : integers ) {
System.out.println( integer );
}
System.out.println(
"*** End of Integer List ***" );
}
public static void main ( final String[] args ) {
App app = new App(); // Instantiate an object to get out of the static context of a "main" method.
app.doIt();
}
}