如何修复主要方法以便从其他方法识别变量 [英] How Do I Fix The Main Method So That It Will Recognize Variables From Other Methods

查看:89
本文介绍了如何修复主要方法以便从其他方法识别变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / *   * 
*在这里写一个类方法的描述。
*
* @author(您的名字)
* @version(版本号或日期)
* /

import java.util。随机;
public class 方法
{

public 方法()
{
int a,b,c,avg = 0 ;
字符串 grade = ;
Random random = new Random();
a = random.nextInt( 101 );
b = random.nextInt( 101 );
c = random.nextInt( 101 );

系统。 out .println( 您的成绩是 + avg + 您收到 +成绩 ;

}


public static int avg( int a, int b, int c){
int avg =(a + b + c)/ < span class =code-digit> 3 ;

return avg;


}


public static 字符串成绩( int avg){
字符串等级;
if (avg > = 90 . 0
grade = A;
其他 如果(avg > = 80 0
grade = B;
其他 如果(avg > = 70 0
grade = C;
其他 如果(avg > = 60 0
grade = D;
else
grade = F;

返回等级;


}
}

解决方案

你没有解决它 - 你会使用另一种技术:

  public   class  MyClass {

private String test = 它的工作原理;

// construtor NEVER执行代码。如果作为参数给出,则只有值被移交。
// 另一个有趣的事情发生在另一个可能被调用的方法(通常是init())。
public MyClass(){
.startTheFun();
}

private void startTheFun(){
// 玩得开心
System.out.println(test);
}

// 主要是入口点 - 而不是更多。所以让我们创建一个对象!
// 主要方法是你的唯一静态东西class。
public void main( String [] args){
new MyClass(); //
}

}


/**
 * Write a description of class Methods here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
import java.util.Random;
public class Methods
{

    public Methods()
    {
       int a, b, c, avg=0;
       String grade="";
       Random random = new Random();
       a = random.nextInt(101);
       b = random.nextInt(101);
       c = random.nextInt(101);

       System.out.println("Your grade is " + avg + " you receive a " + grade);

    }


       public static int avg(int a, int b, int c) {
           int avg = (a + b + c) / 3;

           return avg;


    }


    public static String grade(int avg) {
        String grade;
        if (avg >= 90.0)
        grade = "A";
        else if (avg >= 80.0)
        grade = "B";
        else if (avg >= 70.0)
        grade = "C";
        else if (avg >= 60.0)
        grade = "D";
        else
        grade = "F";

        return grade;


    }
}

解决方案

You do not fix it - you will use another technic:

public class MyClass{

  private String test = "it works";

  // construtor NEVER executes code. only values are handed over if given as arguments.
  // Further fun is happening in another method that might be called (often init() ).
  public MyClass(){
    this.startTheFun();
  }

  private void startTheFun(){
  // have fun
    System.out.println(test);
  }

  // Main is entry point - not more. so let's create an object!
  // the main method is the only static thingy in your class.
  public void main(String[] args){
    new MyClass(); //
  }

}


这篇关于如何修复主要方法以便从其他方法识别变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆