Home > front end >  Underlined, and still not fixable Part2 Question of Awelooo
Underlined, and still not fixable Part2 Question of Awelooo

Time:12-07

I am new in programming, and I think to be a niceful to each other would make this community even better. My goal is to writing the code out to learn more about it.

So before getting rude you should understand that this Website, Stack Overflow is also for beginners! Otherwise nobody would get intrested in this Site.

The issue here is that my scripts are getting underlined with red. Which makes me feel that I am doing something wrong :/

So my picture should explain everything. Lets get into my problem by linking my picture:

enter image description here

package ratespiel;

import java.util.Random;
import javax.swing.JOptionPane

public class Zahlraten {
    private int zufallzahlen;
    private int ratezahl;

    Zahlraten(){
        private int zufallzahlen;
        private int ratezahl;
        Zahlraten() {
            ratezahl = -1;
        }
    }
}

I hope thats it at the end.

CodePudding user response:

First, never call Java Code Script. Java is a compiled language and is not a script, like JavaScript which is interrupted at run-time.

Your code has many issues - like a nested constructor. Also you are declaring variables in your constructor. You are also missing a ; char after your second import statement.

You want to set data members in a constructor (zufallzahlen and ratezahl) - not declare them. You can overload constructors as well - which means that have different parameters.

See this code:

import java.util.Random;
import javax.swing.JOptionPane;

public class Zahlraten {
    private int zufallzahlen;
    private int ratezahl;

    Zahlraten(int zufallzahlen,  int ratezahl ){
        this.zufallzahlen = zufallzahlen;
        this.ratezahl = ratezahl ;
       }

    Zahlraten(int value) {
        ratezahl = value;
    }   
}

CodePudding user response:

You have some sort of nested constructor, which is not correct. You want something like this:

public class Zahlraten {
    private int zufallzahlen;
    private int ratezahl;

    Zahlraten() {
          ratezahl = -1;
        }
    }

I would also recommend reading the Java Tutorials from Oracle to get some basic understanding. Also, confusing to call Java code "scripts".

CodePudding user response:

You must worry about the syntax errors instead. For eg.:

  1. You missed to add a semi-colon at the end of 2nd import statement.

    import javax.swing.JOptionPane
    
  2. You are tying to write a constructor with a constructor. That is not valid.

    Zahlraten() {
         Zahlraten() {}
     }
    
  3. You have declared variables inside constructor as private. You cannot specify access specifiers.
    Here, private is one of an access specifier.

    Zahlraten() {
         private int zufallzahlen; //not valid
         private int ratezahl; //not valid
     }
    

The IDE (at the bottom pane) is trying to identify these issues to you. If you try to solve them one by one you should be able to run your class successfully :) I suggest you can also read more on the Java concepts and practice in your code parallelly.

  •  Tags:  
  • java
  • Related