0

我正在制作一个程序,提示用户输入 3 个整数并打印出选择的最大整数。我目前遇到两个问题。我想知道如何制作程序,以便用户只能从数组中选择整数。我还想知道如何从用户选择的整数中找到并打印出最大的整数。我对编程很陌生,因此感谢所有反馈。

谢谢!

import java.util.Scanner; 
public class Lab14C // name of class file 
{
    public static void main(String [] args)
    {
        int[] array = {0,1,2,3,4,5,6,7,8,9}; 
        for(int i=0; i<array.length; i++) 
        {
            System.out.print(array[i] + " "); 
        }

        System.out.println("\n"); 
        Scanner array1 = new Scanner(System.in);
        System.out.println("What is your first integer? ");
        double array11 = array1.nextInt();

        Scanner array2 = new Scanner(System.in);
        System.out.println("What is your second integer? ");
        double array22 = array2.nextInt();

        Scanner array3 = new Scanner(System.in);
        System.out.println("What is your third integer? ");
        double array33 = array3.nextInt();
        System.out.println("\n");
    }
}
4

3 回答 3

0

I don't think there is a way to force a user to input an element. Few things you could do is :

  • Tell the user he has to select a number in a particular range.
  • Keep the input statement in a loop. If the entered element exists in array , go ahead. Else tell the user to enter again.

Printing the biggest integer can be done using Math.max(double,double) function. For three elements you can try System.out.println("Max of three is "+Math.max(array11,Math.max(array22,array33)))

You can do it yourself if you want instead of built in function like:

if(array1>array2&&array1>array3)
//print max as array1
else if(array2>array1&&array2>array3)
//print max as array2
else //print array3 as max

Also change your element types to int as you are reading integer.

于 2013-10-09T05:21:53.553 回答
0

1) There is no need to create a new Scanner all the time. Just create one Scanner (which I would just call input or scanner or something that makes sense).

2) If you're reading int's why are you storing them in doubles?

3) To check for a certain condition you use if(*condition*) { /*do something */ }. So if you want to check if x is smaller than y you do if(x < y) { /* do something */ }. (In your case you'll want to check if current input is greater than biggest input and if so set the biggest input to current input.)

4) For a sorted array you can use Arrays.binarySearch(array, elementToSearch) which will return the index of the element when found, or a negative number if not found (the negative number is (-(insertionPoint)-1)). (So you can check if the number entered by the user is in the array and keep asking for a new number if is not.)

于 2013-10-09T05:22:34.943 回答
0

1)How I can make the program so that the user can only choose integers from the array.?

您正在声明array变量,int[]因此它仅存储整数值。每当您从 this 中检索值时array,它只会返回int值,因此您不必担心它。

2)how to find and print out the biggest integer from the ones that the user chose.?

为了从一组值中找到最大值或最小值,Java 提供了一个函数名Math#max()。你可以像这样使用它:

int maxValue = Math.max(Math.max(array11,array22),array33);

是数学库的文档。

于 2013-10-09T05:23:42.457 回答