if statement - While/if Loop is not working in class -


i'm learning java , i'm having issue if code not running.

in following code i'm trying determine if number (variable num) triangle number (1,3, 6, 10 etc). code should run through , give "is triangle". keeps spitting out null.

i understand not effective way code, trying learn how use classes.

public class helloworld {   public static void main(string[] args) {     class numbershape {         int num = 45;         int tri = 0;         int triplus = 0;         string triresult;          public string triangle() {              while (tri < num) {                 if (tri == num) {                     triresult =  "is triangle";                     system.out.println("is triangle");                 } else if (tri + (triplus + 1) > num){                     triresult =  "is not triangle";                 } else {                     triplus++;                     tri = tri + triplus;                 }             }             return triresult;         }     }      numbershape result = new numbershape();     system.out.println(result.triangle());     } } 

thanks provided.

try code :

public class helloworld {   public static void main(string[] args) {             class numbershape {                 int num = 10;//try other numbers                 int tri = 0;                 int triplus = 0;                 int res = 0;                 string triresult =  "is not triangle";                  int[] tab= new int[num];                    public string triangle() {                      //to calculate triangle numbers                       for(int = 0; i<num; i++){                          res = res + i;                          tab[i]=res;                      }                       //to check if num triangle or not                      for(int = 0; i<tab.length; i++){                          system.out.println(">>  " + + " : " + tab[i]);                          if(tab[i]== num){                              triresult =  num + " triangle";                              break;//quit if condition checked                          }else{                              triresult =  num + " not triangle";                          }                      }                      return triresult;                 }             }              numbershape result = new numbershape();             system.out.println(result.triangle());             } } 

hope helps.


Comments