Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Калькулятор. Дубль 2. #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RomanKalin27 wants to merge 2 commits into Yandex-Practicum:master
base: master
Choose a base branch
Loading
from RomanKalin27:master
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scan = new Scanner(System.in);
System.out.println("На сколько человек вы хотели бы разделить чек?");
try {
Copy link

@MagicUnderHood MagicUnderHood Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно try-catch разместить внутри бесконечного цикла while, тогда при вводе строки пользователю будет позволено снова ввести число, без перезапуска программы (как сделано с числами меньше 1)

int numPeople = scan.nextInt();
while (1 < 4) {
Copy link

@MagicUnderHood MagicUnderHood Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно вместо 1 < 4 писать просто true

if (numPeople > 1) {
break;
}
System.out.println("Ошибка. Невозможно разделить чек на данное колличество людей. Попробуйте снова.");
numPeople = scan.nextInt();
}

double check = calc.calculator() / numPeople;
System.out.println("Каждый человек должен заплатить: " + check + calc.ending(check));
} catch (Exception e){
System.out.println("Ошибка. Попробуйте снова.");
}
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/calc.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.Scanner;

public class calc {

public static double calculator() {
Scanner scan2 = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
Scanner scan4 = new Scanner(System.in);
for (String i = "Добавленные товары:"; 1 != 0; i = i) {
Copy link

@MagicUnderHood MagicUnderHood Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 != 0 можно заменить на true, а i = i убрать. Может, здесь for можно заменить на цикл while?

for (double j = 0; j >= 0; j = j) {
System.out.println("Пожалуйста, введите название товара.");
String itemName = scan2.nextLine();
System.out.println("Пожалуйста, введите цену товара в формате 'рубли.копейки' [10.45, 11.40]");
double itemCost = scan3.nextDouble();
System.out.println("Товар добавлен в список.");
i = i + "\n" + itemName + " " + itemCost + ending(itemCost);
j = j + itemCost;
System.out.println("Хотите ли бы вы завершить покупки?");
String answer = scan4.nextLine();
String stopWord = "Завершить";
if (answer.equalsIgnoreCase(stopWord)) {
System.out.println(i);
System.out.println("Общая сумма покупок: " + j + ending(j));
return j;
}
}
}
}
public static String ending(double j){
if(Math.floor(j) % 100 == 11 ^ Math.floor(j) % 100 == 12 ^ Math.floor(j) % 100 == 13 ^ Math.floor(j) % 100 == 14){
Copy link

@MagicUnderHood MagicUnderHood Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.floor(j) % 100 повторяется. можно посчитать один раз, записать в переменную и использовать её
Вместо ^ чаще используют написание ||
Можно условие заменить на Math.floor(j) % 100 >= 11 && Math.floor(j) % 100 <= 14

String stringe = " рублей";
return stringe;
Copy link

@MagicUnderHood MagicUnderHood Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно убрать переменную, написав просто return " рублей";, и ниже тоже

}
else {
double summ = Math.floor(j) % 10;
int remainder = (int) summ;
switch (remainder) {
case 1:
String string1 = " рубль";
return string1;
case 2:
String string2 = " рубля";
return string2;
case 3:
String string3 = " рубля";
return string3;
case 4:
String string4 = " рубля";
return string4;
default:
String stringdef = " рублей";
return stringdef;
}
}
}
}

AltStyle によって変換されたページ (->オリジナル) /