理解数组下标——有趣程序
1.让用户输入【0,9】范围内整数不定数个
2.统计,输出每个数次数
3.输入-1结束
思路——数据,算法
1.数据
1.输入的数
2.记录每一种数出现次数
程序
<p class="p1"><span class="s1">import</span> java.util.Scanner;</p><p class="p2">
</p><p class="p3">public<span class="s2"> </span>class<span class="s2"> Main {</span></p><p class="p2">
</p><p class="p1"> <span class="s1">public</span> <span class="s1">static</span> <span class="s1">void</span> main(String[] <span class="s3">args</span>) {</p><p class="p2"> </p><p class="p1"><span> </span> Scanner <span class="s3">in</span> = <span class="s1">new</span> Scanner(System.<span class="s4">in</span>);</p><p class="p2"><span> </span> </p><p class="p1"><span> </span> <span class="s1">int</span> <span class="s3">x</span>;</p><p class="p4">// 创建数组后没有也没必要初始化数组——JAVA默认初始化为0</p><p class="p1"><span> </span> <span class="s1">int</span>[] <span class="s3">numbers</span> = <span class="s1">new</span> <span class="s1">int</span> [10];<span> </span> </p><p class="p4">//<span> </span> 测试一下——下面这个for循环可以删掉</p><p class="p1"><span> </span> <span class="s1">for</span> ( <span class="s1">int</span> <span class="s3">i</span> = 0; <span class="s3">i</span> < <span class="s3">numbers</span>.<span class="s4">length</span>; <span class="s3">i</span>++ )</p><p class="p1"><span> </span> {</p><p class="p1"><span> </span><span> </span> System.<span class="s4">out</span>.println(<span class="s3">numbers</span>[<span class="s3">i</span>]);</p><p class="p1"><span> </span> }</p><p class="p2"><span> </span> <span> </span> </p><p class="p1"> <span class="s3">x</span> = <span class="s3">in</span>.nextInt();</p><p class="p2"> </p><p class="p1"> <span class="s1">while</span> (<span class="s3">x</span> != -1)</p><p class="p1"> {</p><p class="p4">// <span> </span> 防止用户输入11等会出错的数</p><p class="p1"> <span> </span> <span class="s1">if</span> ( <span class="s3">x</span> >= 0 && <span class="s3">x</span><=9 )</p><p class="p1"> <span> </span> {</p><p class="p4">// <span> </span><span> </span> 数组参与运算</p><p class="p1"> <span> </span><span> </span> <span class="s3">numbers</span>[<span class="s3">x</span>] ++;</p><p class="p1"> <span> </span> }</p><p class="p2"> <span> </span> </p><p class="p1"> <span> </span> <span class="s3">x</span> = <span class="s3">in</span>.nextInt();</p><p class="p1"> }</p><p class="p4">// 遍历数组做输出</p><p class="p1"> <span class="s1">for</span> ( <span class="s1">int</span> <span class="s3">i</span> = 0; <span class="s3">i</span> < <span class="s3">numbers</span>.<span class="s4">length</span> ; <span class="s3">i</span>++ )</p><p class="p1"> {</p><p class="p1"> <span> </span> System.<span class="s4">out</span>.println(<span class="s3">i</span>+<span class="s5">":"</span>+<span class="s3">numbers</span>[<span class="s3">i</span>]);</p><p class="p1"> }</p><p class="p1"> }</p><p class="p1">}</p>
留意
1.创建数组后没有也没必要初始化数组——JAVA默认初始化为0
// 创建数组后没有也没必要初始化数组——JAVA默认初始化为0
int[] numbers = new int [10];
// 测试一下
for ( int i = 0; i < number.length; i++ )
{
System.out.println(numbers[i]);
}
输出:
0
0
0
0
0
0
0
0
0
0
输入:
1 1 2 3 3 3 4 -1
输出:
0:0
1:2
2:1
3:3
4:1
5:0
6:0
7:0
8:0
9:0