public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(
System.in );
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.printf("%s%d%s%dx%s%dy", sign(a, true), a, sign(b), b, sign(c), c);
}
private static String sign(int n) {
return sign(n, false);
}
private static String sign(int n, boolean first) {
return n < 0 || first ? "" : "+";
}
}
ОракулОракул (62081)
2 недели назад
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(
System.in );
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.printf("%s%sx%sy", format(a, true), format(b), format(c));
}
private static String format(int n) {
return format(n, false);
}
private static String format(int n, boolean first) {
return n < 0 || first ? "" + n : "+" + n;
}
}
1
2
-3
Вывод:
1+2x+-3y
Вывод ошибочный, должно выводится: 1+2х-3у
Подскажите, что я сделал не так.
Код ниже:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner( System.in );
String a = sc.nextLine();
String b = sc.nextLine();
String c = sc.nextLine();
String asec = "";
String bsec = "";
String csec = "";
int botr = 0;
int cotr = 0;
int cona = 0;
int bona = 0;
for(int i = 0, j = 0; i!=(b.length()-1) && j!=(c.length()-1); i++, j++){
if((b.charAt(i)) == '-'){
botr+=1;
}
else if((c.charAt(j)) == '-'){
cotr+=1;
}
}
if(a.equals("0")){
asec = "";
cona+=1;
}
else{
asec = a;
}
if(cona == 1){
if(b.equals("1")) {
bsec = "x";
}
else if(b.equals("0")){
bsec = "";
bona+=1;
}
else{
bsec = b+"x";
}
}
else{
if(botr==1){
if(b.equals("-1")) {
bsec = "-" + "x";
}
else if(b.equals("0")){
bsec = "";
bona+=1;
}
else{
bsec = b + "x";
}
}
else{
if(b.equals("1")) {
bsec = "+" + "x";
}
else if(b.equals("0")){
bsec = "";
bona+=1;
}
else{
bsec = "+" + b + "x";
}
}
}
if(cona == 1 && bona == 1){
if(c.equals("1")) {
csec = "y";
}
else if(c.equals("0")){
csec = "";
}
else{
csec = b+"y";
}
}
else{
if(cotr == 1){
if(c.equals("-1")) {
csec = "-" + "y";
}
else if(c.equals("0")){
csec = "";
}
else{
csec = c + "y";
}
}
else{
if(c.equals("1")) {
csec = "+" + "y";
}
else if(c.equals("0")){
csec = "";
}
else{
csec = "+" + c + "y";
}
}
}
System.out.println(asec+bsec+csec);
}
}