Java Basic Objecty -ориентированный калькулятор вознагражденияJAVA

Программисты JAVA общаются здесь
Гость
Java Basic Objecty -ориентированный калькулятор вознаграждения

Сообщение Гость »

Я начинаю узнать больше о Java, и я пытаюсь кодировать калькулятор боевых действий, который приобретает пользовательский ввод, и показывает, насколько совет будет на %10 и %20 от общего числа. Я получаю единую ошибку «Не могу сделать статическую ссылку на нестатическую метод», которую я не могу решить.public class Gratuity{

//variables
private double total = 0;
private double grat1 = 0;
private double grat2 = 0;

public Gratuity(float value){
total = value;
}

start getters and setters
public double getTotal() {
return total;
}

//method to do the calculations
public void calcGrat(){
grat1 = total * .10;
grat2 = total * .20;
}
public double getGrat1(){
return grat1;
}
}
< /code>

и класс с основным методом: < /p>

import java.util.InputMismatchException;
import java.util.Scanner; //import package to use the scanner input function

//TestGrat main class contains method
public class TestGrat {

Scanner keyboard = new Scanner(System.in);

//method to prompt user for total, double is total
public void askForInput(){

try{
System.out.println("Enter the total amount of your bill");
total = keyboard.nextDouble();
}
catch(InputMismatchException e){
System.err.printf("Error, please try again. Program will now close");
System.exit(0);
}

}
public Scanner getKeyboard() {
return keyboard;
}
public void setKeyboard(Scanner keyboard) {
this.keyboard = keyboard;
}

//main method
public static void main(String[] args){

// asks for input in float form

float value = askForInput();

//Creating the gratCalc object and storing value as a float (total)
Gratuity gratCalc = new Gratuity(value);

// get the total value and set as float
float tot = (float)gratCalc.getTotal();

// converting the float value into string
System.out.println("You have entered: " + Float.toString(tot));
gratCalc.calcGrat(); //sets grat

// Displaying the options to user
System.out.println("Below are the tips for %10 as well as %20 ");

//getting the value and then displaying to user with toString
float getNum = (float) gratCalc.getGrat1();
float getNum1 = (float) gratCalc.getGrat2();

// using the value of getNum as float to put into toString
System.out.println( "For %10: " + Float.toString(getNum));
System.out.println(" For %20: " + Float.toString(getNum1));
}

}
< /code>

Любая помощь будет оценена. Спасибо!

Подробнее здесь: https://stackoverflow.com/questions/340 ... calculator

Вернуться в «JAVA»