Problem:
Input: Power consumption.
Output: Cost of electricity.
Code:
Main method:
Import Java.util.Scanner;
public class Test {public
static void Main (String[]args) {
System.out.print ("Please enter the amount of the Electi C: "//In the class written in the function inside System.out.print () can be identified
Scanner reader=new Scanner (system.in);
Double amount=reader.nextdouble ();
Computer number=new Computer ();
Number.money (amount);
}
Other:
public class Computer {public
void money (double amount) {
double price=0;
if (amount<=90&&amount>=1) {
price=amount*0.6;
System.out.printf ("The price of the electic is:%5.2f", price);//This place is the output is printf
}
else if (amount<=150 &&amount>=91) {
price=90*0.6+ (amount-90) *1.1;
System.out.printf ("The price of the electic are:%5.2f", price);
}
else if (amount>151) {
price=90*0.6+60*1.1+ (amount-151) *1.7;
System.out.printf ("The price of the electic are:%5.2f", Price);}}
Run Result:
Summarize:
In 1,java, the output with SYSTEM.OUT.PRINTLN can be written in the constructor of the class, then called in the main function, or written directly in the main function, but cannot be used directly in the class.
Class a{
SYSTEM.OUT.PRINTLN ()//No.
}
Best Answer Class a{
Member variable;
Construction method;
Common methods;
Internal class;
}
In a class, you can have up to a maximum of these. If you want to use your code above, you have to add a curly brace. Let him become a normal block, ordinary block is the common method to remove some of the previous statements, also belong to the common method. Class A{{System.out.println (); } }
2.printf can be exported in format
Println can be wrapped in the output after the return line
Print is a simple output