英文:
How to call a method from another method to get the withdrawal value divide into $20 notes and $50 notes
问题
import java.util.Scanner;
public class Main {
public static int Input() {
Scanner input = new Scanner(System.in);
int result;
return result = input.nextInt();
}
static void getValue() {
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money / 50);
for (int i = 1; i <= (money / 50); i++) {
y++;
}
bill20 = (money - (y * 50)) / 20;
for (int i = 1; i <= bill20; i++) {
x++;
}
System.out.println("Here is " + x + " $20 notes and " + y + " $50 notes.");
}
public static void run() {
System.out.print("Enter the amount of money: ");
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
if (money > 100 && money % 50 != 0 && money % 20 != 0) {
bill50 = (money / 50) - 1;
for (int i = 1; i <= bill50; i++) {
y++;
}
bill20 = (money - (y * 50)) / 20;
for (int i = 1; i <= bill20; i++) {
x++;
}
int sum = (x * 20) + (y * 50);
if (sum == money) {
System.out.println("Here is " + x + " $20 notes and " + y + " $50 notes.");
} else {
getValue(); // Something went wrong here
}
}
}
public static void main(String[] args) {
run();
}
}
输出:
Enter the amount of money: 110
Here is 3 $20 notes and 1 $50 notes.
Enter the amount of money: 190
Here is 2 $20 notes and 3 $50 notes.
当输入 190 时,系统会输出 "Here is 2 $20 notes and 3 $50 notes."。
英文:
currently I'm working on an assignment where I take the user's input, which is the amount of money that they want to withdraw and the output will be that money divide into bills of 20 and bills of 50. I have complete the conditions for other cases but I got stuck here, with numbers 110 and 190. I'm unable to call the getValue method from the run method, can you guys help me
import java.util.Scanner;
public class Int{
public static int Input(){
Scanner input = new Scanner(System.in);
int result;
return result= input.nextInt();
}
static void getValue(){
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money/50);
for(int i=1; i<= (money/50); i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i<=bill20; i++){
x++;
}
System.out.println("Here is " + x + " $20 notes and "+y+ " $50 notes. ");
}
public static void run(){
System.out.print("Enter the amount of money: ");
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
if(money > 100 && money % 50 != 0 && money % 20 != 0 ){
bill50 = (money/50)-1;
for(int i=1; i<=bill50; i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i<=bill20; i++){
x++;
}
int sum = (x*20)+(y*50);
if (sum == money){
System.out.println("Here is " + x + " $20 notes and "+y+ " $50 notes. ");
} else{
getValue(); //Something went wrong here
}
}
}
//Printing the run method
public static void main(String[] args){
run();
}
}
Output:
Enter the amount of money: 110
Here is 3 $20 notes and 1 $50 notes.
Enter the amount of money: 190
When I entered 190, the system didn't print anything
答案1
得分: 0
以下是您要翻译的代码内容:
Buddy,你的问题已经解决了,请尝试这段代码:
import java.util.Scanner;
class Int {
int result;
public void Input() {
System.out.printf("输入金额:");
Scanner input = new Scanner(System.in);
result = input.nextInt();
}
public void getValue() {
int money = result;
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money / 50);
for (int i = 1; i <= (money / 50); i++) {
y++;
}
bill20 = (money - (y * 50)) / 20;
for (int i = 1; i <= bill20; i++) {
x++;
}
System.out.println("这里有 " + x + " 张 20 美元钞票和 " + y + " 张 50 美元钞票。");
}
public void run() {
System.out.print("输入金额:");
int money = result;
int bill20, bill50;
int x = 0, y = 0;
if (money > 100 && money % 50 != 0 && money % 20 != 0) {
bill50 = (money / 50) - 1;
for (int i = 1; i <= bill50; i++) {
y++;
}
bill20 = (money - (y * 50)) / 20;
for (int i = 1; i <= bill20; i++) {
x++;
}
int sum = (x * 20) + (y * 50);
if (sum == money) {
System.out.println("这里有 " + x + " 张 20 美元钞票和 " + y + " 张 50 美元钞票。");
} else {
getValue(); // 这里出了些问题
}
}
}
public static void main(String[] args) {
Int i = new Int();
i.Input();
i.getValue();
i.run();
}
}
英文:
Buddy your issue is fixed, try this code
import java.util.Scanner;
class Int{
int result;
public void Input(){
System.out.printf("Enter the money : ");
Scanner input = new Scanner(System.in);
result= input.nextInt();
}
public void getValue(){
int money = result;
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money/50);
for(int i=1; i<= (money/50); i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i<=bill20; i++){
x++;
}
System.out.println("Here is " + x + " $20 notes and "+y+ " $50 notes. ");
}
public void run(){
System.out.print("Enter the amount of money: ");
int money = result;
int bill20, bill50;
int x = 0, y = 0;
if(money > 100 && money % 50 != 0 && money % 20 != 0 ){
bill50 = (money/50)-1;
for(int i=1; i<=bill50; i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i<=bill20; i++){
x++;
}
int sum = (x*20)+(y*50);
if (sum == money){
System.out.println("Here is " + x + " $20 notes and "+y+ " $50 notes. ");
} else{
getValue(); //Something went wrong here
}
}
}
//Printing the run method
public static void main(String[] args){
Int i = new Int();
i.Input();
i.getValue();
i.run();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论