package 重复;
import java.util.Arrays;
import java.util.Scanner;public class Five
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
 for(int i=0;i<2;i++)
 {
 int n=input.nextInt();
 System.out.println("输出   "+n+"  个重复的数字");
 int a=input.nextInt();
 System.out.println("输出的重复数字为    "+a);
 
 }

 

int[] chf=new int[n];
for(int i=0;i<n;i++)
{
chf[i]=a;

}
System.out.println(chf);

}

解决方案 »

  1.   

    变量的作用范围
    把n和a定义在for循环外,如果定义在for循环内部,那么出了for的花括号,n和a的生命周期就结束了,
    所以后面
    int[] chf=new int[n]; //这里的n就无效了,因为n的作用范围只是在for循环的花括号里
      

  2.   

    package test1;import java.util.Arrays;
    import java.util.Scanner;public class Again {
    public static void main(String[] args) 
    {
    int n = 0;
    int a = 0;
    Scanner input = new Scanner(System.in);
    for (int i = 0; i < 2; i++) {
    n = input.nextInt();
    System.out.println("输出 " + n + " 个重复的数字");
    a = input.nextInt();
    System.out.println("输出的重复数字为 " + a); } int[] chf = new int[n];
    for (int i = 0; i < n; i++) 
    {
    chf[i] = a;
    System.out.println("aiwo");
    System.out.println(chf[i]);
    } }
    }
    结果还是出不来啊
    输出没反应
      

  3.   

    for (int i = 0; i < 2; i++) {~~~~~你为什么要循环两次,你一次循环不就获得了你要的参数的值吗?