Repository navigation
1st java module #127
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
1st java module #127
Changes from all commits
10146c2
1b476c1
276f7d3
aaf6904
671918a
b66f80e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
| public class Calc { | ||
|
|
||
| public static void Calculator() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| String result = ""; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Студия подсказывает, что, хотя переменная инициализируется, она не используется после этого |
||
| double count = 0; | ||
| boolean cont = true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно избавиться от переменной, если писать while(true), а в месте, где нужно выйти из цикла - писать break; |
||
| //создаем бесконечный цикл вызова товара | ||
| while (cont) { | ||
| System.out.println("Введите название товара"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно дописать "Введите Завершить, чтобы закончить ввод", иначе пользователь не поймёт, что следует ввести для выхода |
||
| String res = scanner.next(); | ||
| // устанавливаем слово для остановки цикла | ||
| if (res.toUpperCase(Locale.ROOT).equals("ЗАВЕРШИТЬ")) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Или можно использовать equalsIgnoreCase() |
||
| cont = false; | ||
| } else { | ||
| result += res + "\n"; | ||
| System.out.println("Введите стоимость в формате: \'рубли.копейки' "); | ||
| String temp = scanner.next(); | ||
| // проверка на то чтобы пользователь вводил только int стоимость | ||
| if (!checkPrice(temp)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать scanner.hasNextFloat() |
||
| System.out.println("Некорректные данные"); | ||
| } else { | ||
| System.out.println("Товар " + res + " успешно добавлен"); | ||
| count += Double.parseDouble(temp); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать scanner.nextFloat() |
||
| } | ||
| } | ||
| } | ||
| //Вывод будет одинаковый вне зависимости от того, какие данные укажет пользователь | ||
|
|
||
| double bill = count / Users.users; | ||
|
|
||
| String resultMessage = ("Общая стоимость: %.2f RUB"); | ||
| System.out.println(String.format(resultMessage, count)); | ||
|
|
||
| System.out.println("Сумма на одного человека: " + String.format("%.2f", bill) + " " + rubles(bill)); | ||
| } | ||
| //реализация проверки | ||
| public static boolean checkPrice(String next) { | ||
| boolean isDigit = true; | ||
| for (int i = 0; i < next.length(); i++) { | ||
| if (!Character.isDigit(next.charAt(i)) && next.charAt(i) != '.') { | ||
| isDigit = false; | ||
| break; | ||
| } | ||
| } | ||
| return isDigit; | ||
|
|
||
| } | ||
| public static String rubles(double a) { | ||
| int rub = (int) Math.floor(a); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично, что используешь переменную, всё сделано правильно |
||
| if (rub % 100 > 5 && rub <= 20) { | ||
| return "рублей"; | ||
| } else if (rub % 10 == 1) { | ||
| return "рубль"; | ||
| } else if (rub % 10 > 1 && rub % 10 < 5) { | ||
| return "рубля"; | ||
| } | ||
| return "рублей"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| //main используем только для обращения к методам | ||
| Users.persons(); | ||
| Calc.Calculator(); | ||
| System.out.println("Спасибо за пользование нашим приложением!"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Users { | ||
| public static int users; | ||
|
|
||
| public static void persons() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| //вводим бесконечный цикл, чтобы пользователь мог ввести правильное значение | ||
|
|
||
| while (true) { | ||
| System.out.println("Выберите количество человек"); | ||
|
|
||
| if (scanner.hasNextInt()) { | ||
| users = scanner.nextInt(); | ||
| if (users>1) { | ||
| System.out.println("Колличество пользователей = " + users); | ||
| break; | ||
| } | ||
| if(users<=1){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно поставить else к предыдущему if вместо этого кода |
||
| System.out.println("Введите корректные данные. Значение должно быть больше 1"); | ||
| } | ||
| } else { | ||
| System.out.println("Введите корректные данные"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно код ниже выполнять в отдельном методе, назвать его так, чтобы он отражал работу внутри, например, inputGoods, и вызывать этот метод в Main