简体   繁体   English

我的 System.out.println 代码的两行不起作用,我不知道为什么

[英]Two lines of my System.out.println code do not work and I cannot figure out why

I recently made a simple calculator that calculates the perimeter and area of a rectangle when you give it the measurements of two sides.我最近做了一个简单的计算器,当你给它两个边的测量值时,它可以计算矩形的周长和面积。 However, two of my lines of System.out.println are not working.但是,我的两行 System.out.println 不起作用。 How can I fix this?我怎样才能解决这个问题?

Here is the code:这是代码:

import java.util.Scanner;

class Rectangle
{
    static int n;
    static int m;
    Scanner s= new Scanner(System.in);
//The below two System.out.println lines do not work. How do I fix this?
    System.out.println("Enter the width:")
    n = s.nextInt();
    System.out.println("Enter the length:");
    m = s.nextInt();
    public static void main(String args[])
    {
        int Area;
        Area=n*m;
        System.out.println("Area = " + Area);
        return;
    }
    private static int solvePerimeter;
    {   
        int Perimeter; 
        Perimeter = 2*(m+n);
        System.out.println("Perimeter = " + Perimeter);

Print statements should be inside a function.打印语句应位于 function 内。
Change your code to:将您的代码更改为:

import java.util.Scanner;

class Rectangle
{
    static int n;
    static int m;
    public static void main(String args[])
    {
    Scanner s= new Scanner(System.in);
    System.out.println("Enter the width:")
    n = s.nextInt();
    System.out.println("Enter the length:");
    m = s.nextInt();
    }

You also need to declare two separate functions for area and perimeter and call from main method.您还需要为面积和周长声明两个单独的函数,并从主方法调用。

Your System.out.println("Enter the width:");你的System.out.println("Enter the width:"); should be inside a method.应该在一个方法里面。 Other than any variable declaration, everything should be inside methods.除了任何变量声明,一切都应该在方法内部。

import java.util.Scanner;

class Rectangle
{
    static int n;
    static int m;

    Scanner s= new Scanner(System.in);
//The below two System.out.println lines do not work. How do I fix this?
    public void readArguments() {
       System.out.println("Enter the width:");
       n = s.nextInt();
       System.out.println("Enter the length:");
       m = s.nextInt();
   }

    public static void main(String args[])
    {
        int Area;
        readArguments();
        Area=n*m;
        System.out.println("Area = " + Area);
        return;
    }
    private static int solvePerimeter;
    {   
        int Perimeter; 
        Perimeter = 2*(m+n);
        System.out.println("Perimeter = " + Perimeter);

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

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