3.7.1 Calculate the electricity bill

Source: Internet
Author: User



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





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.