简体   繁体   English

如何使此循环运行?

[英]How can I make this loop run?

My first for loop is not working. 我的第一个for循环无法正常工作。 I tried while loop to but its also not worked. 我尝试了while循环,但是也没有用。 What is wrong in this code İnside of for loop program create an object for the inside of object array and take variables from the user 在这段代码中有什么问题?for循环程序的旁边为对象数组的内部创建一个对象,并从用户那里获取变量

package kisi_ödev;


import java.util.Scanner;//input almak için kullanılacak olan sınıf için çağrılan kütüphana   class kisi   //kisi sınıfı {   public long kn;          //private değişkenkleri public yaptım         public String ad, soyad;         public int yas; } /**  *  * @author OZAN  */ public class Kisi_ödev {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int size_of_objectList=1;//nesne dizisinin boyutu
        Scanner input=new Scanner(System.in);//Scanner sınıfı yeni nesnesi
        kisi bilgiler[]=new kisi[size_of_objectList];
        for(int i=0;i<bilgiler.length;i++)//This is not working
        {
            bilgiler[i]=new kisi();
            System.out.println("kimlik numarasını giriniz:");
            bilgiler[i].kn=input.nextLong();
            if(bilgiler[i].kn==0){
                for(int j=0;j<bilgiler.length;j++){
                    if(bilgiler[j]!=null){

                    System.out.println("****************");
                    System.out.print("kimlik numrası: ");
                    System.out.print(bilgiler[j].kn+"   ");
                    System.out.print("İsim: ");
                    System.out.print(bilgiler[j].soyad+", ");
                    System.out.print(bilgiler[j].ad+"  ");

                    System.out.println(bilgiler[j].yas+"  ");
                    System.out.println("****************");

                    }
                }

                System.out.println("shutdown");
                break;
                }
            System.out.println("İsminizi giriniz:");
            bilgiler[i].ad=input.next();
            System.out.println("Soyisminizi giriniz:");
            bilgiler[i].soyad=input.next();
            System.out.println("Yaşınızı giriniz:");
            bilgiler[i].yas=input.nextInt();

        }
    }
}

You must define the class, and you should ensure inputs are long, int ... like you want; 您必须定义类,并且应确保输入是长整型的,如您所愿; otherwise, your program will end on exception. 否则,您的程序将异常终止。

This is a fix version of your source code: 这是源代码的修复版本:

package kisi_ödev;

import java.util.Scanner;

public class LoopTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int size_of_objectList = 2;//nesne dizisinin boyutu
        Scanner input = new Scanner(System.in);//Scanner sınıfı yeni nesnesi
        kisi bilgiler[] = new kisi[size_of_objectList];

        try {
            for (int i = 0; i < bilgiler.length; i++)//This is not working
            {
                bilgiler[i] = new kisi();
                System.out.println("kimlik numarasını giriniz:");
                bilgiler[i].kn = input.nextLong();
                if (bilgiler[i].kn == 0) {
                    for (int j = 0; j < bilgiler.length; j++) {
                        if (bilgiler[j] != null) {

                            System.out.println("****************");
                            System.out.print("kimlik numrası: ");
                            System.out.print(bilgiler[j].kn + "   ");
                            System.out.print("İsim: ");
                            System.out.print(bilgiler[j].soyad + ", ");
                            System.out.print(bilgiler[j].ad + "  ");

                            System.out.println(bilgiler[j].yas + "  ");
                            System.out.println("****************");

                        }
                    }

                    System.out.println("shutdown");
                    break;
                }
                System.out.println("İsminizi giriniz:");
                bilgiler[i].ad = input.next();
                System.out.println("Soyisminizi giriniz:");
                bilgiler[i].soyad = "Yaşınızı giriniz:";
                bilgiler[i].yas = input.nextInt();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Because I don't have a kisi class, I created a sample one: 因为没有kisi类,所以创建了一个示例:

class kisi {

    public long kn;
    public String ad;
    public String soyad;
    public int yas;

}

You defined the count of loop with your size_of_objectList variable; 您使用size_of_objectList变量定义了循环计数; I updated it to 2 in my version. 我在我的版本中将其更新为2。

This is an output sample: 这是一个输出样本:

kimlik numarasını giriniz:
1
İsminizi giriniz:
2
Soyisminizi giriniz:
3
Yaşınızı giriniz:
4
kimlik numarasını giriniz:
0
****************
kimlik numrası: 0   İsim: 3, 2  4  
****************
****************
kimlik numrası: 0   İsim: 3, 2  4  
****************
shutdown

We can perfectly see the 2 loops, and the shutdown when answering 0 to the first input of a loop. 我们可以完美地看到2个循环,以及对循环的第一个输入回答0时的关闭状态。

Thanks to the try/catch, you will see that parsing exception makes your program fails; 感谢try / catch,您将看到解析异常使您的程序失败。 for instance typing 'not a numerical value': 例如输入“非数值”:

kimlik numarasını giriniz:
not a numerical value
java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextLong(Scanner.java:2222)
    at java.util.Scanner.nextLong(Scanner.java:2182)
    at kisi_ödev.LoopTest.main(LoopTest.java:20)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM