아딜렛 2024. 5. 27. 13:11

결과값 전부 0되는 이

public void input() {//input(매개변수 비어도돼)
		
		Scanner sc = new Scanner(System.in);
		
		int w,h;// 지역변수로 설정되어있어서 값 인식이 안됨 ㅠㅠ
		
		System.out.println("가로?");//10
		w =sc.nextInt();
		
		System.out.println("세로?");//20
		h =sc.nextInt();
		
		//return; void는 안 써도 돼
	}

 

▶해결: 

import java.util.Scanner;

public class Rect {
	int w,h;//복도에 빼놓은 물통 전역변수(글로벌변수)=인스턴스 변수
	public void input() {//input(매개변수 비어도돼)
		
		Scanner sc = new Scanner(System.in);
		
		
		
		System.out.println("가로?");//10
		w =sc.nextInt();

 

0값이 쓰레기 값인데 지우는 방법

이전 답

public class Sutja {
	//선언
	int su;
	int cnt;
	//입력
	public void input() {
		Scanner sc= new Scanner(System.in);
		
		////////////(중략)


		}
	}
	public void print(){
		System.out.println(cnt);// 피어리뷰: cnt가 쓰레기 값이어서 0이 계속나오는거 같다.

	}


}

▶해결

public void print(){
		System.out.println(su);//으로 변경함 0 삭제 완료

	}