English 中文(简体)
如何读到 lo体内的价值?
原标题:how to read a value inside a loop?
  • 时间:2012-05-06 05:50:03
  •  标签:
  • java

i am trying to run a very simple java program. i want to write a program that reads 10 integers and that the programs finds witch one is the maximum of them. i wonder if is possible that inside a loop i can read the 10 values.

Scanner input = new Scanner (System.out);
    int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
        System.out.print(" please enter the numer "  +i);
             ===>num[i] = input.nextInt();// 

i am trying to find the way to do it without using an array, since i haven t see this in school yet. any idea how to do it inside a loop? or is just no possible to do?

最佳回答

For that you will have to create int array of 10 length and then read that intvalues in loop and process further. Example :-

Scanner input = new Scanner (System.out).useDelimiter("
");
int values[] = new int[10];
.
.
.


for ( int i = 0 ; i < values.length ; i++ ){
     System.out.print(" please enter the numer "  +i);
     values[i] = input.nextInt();
}
问题回答

保证这样做是可能的。

All you have to do is keep the current maximum value, and then compare it to the value entered by the user for every new value he enters. You can use the for loop to make sure it runs exactly 10 times.

如果你们需要的是最大的价值,你就不需要储存所有10项投入。 因此,如果没有阵列,这是可能的,而且你不需要10个分类变量。

(从一线来看,你发现,你可以一度通过扫描找到一个阵列中的最大位置。) 然后,你再不需要阵列。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签