首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Centroid不接受用户输入的代码

Centroid不接受用户输入的代码
EN

Stack Overflow用户
提问于 2021-02-16 04:47:39
回答 2查看 59关注 0票数 0

我目前正在处理一个项目,在该项目中,我需要获取用户输入,然后使用该输入计算三角形的质心。三角形的长度来自用户的输入。由于某些原因,公式不接受用户输入。

代码语言:javascript
复制
import java.util.Scanner;

public class Centroid {

    public static void main(String[] args) {
        // coordinate of the vertices 
        Scanner bucky = new Scanner(System.in);
        System.out.println("Please enter variables for x1:");
        String x1 = bucky.nextLine();
        System.out.println("Please enter variables for x2:");
        String x2 = bucky.nextLine();
        System.out.println("Please enter variables for x3:");
        String x3 = bucky.nextLine();
        System.out.println("Please enter variables for y1:");
        String y1 = bucky.nextLine();
        System.out.println("Please enter variables for y2:");
        String y2 = bucky.nextLine();
        System.out.println("Please enter variables for y3:");
        String y3 = bucky.nextLine();
        
     
      
        // Formula to calculate centroid 
        float x = (x1 + x2 + x3) / 3; 
        float y = (y1 + y2 + y3) / 3; 
      
        System.out.println("Centroid = "
        + "(" + x + ", " + y + ")"); 
    }

}
EN

回答 2

Stack Overflow用户

发布于 2021-02-16 05:31:19

您使用字符串作为质心值,应使用浮点型、整型、双精度或短整型等数值变量,这就是公式不起作用的原因

尝试浮点变量

代码语言:javascript
复制
public class Centroid {

    public static void main(String[] args) {
        // coordinate of the vertices 
        Scanner bucky = new Scanner(System.in);
        System.out.println("Please enter variables for x1:");
        float x1 = bucky.nextFloat();
        System.out.println("Please enter variables for x2:");
        float x2 = bucky.nextFloat();
        System.out.println("Please enter variables for x3:");
        float x3 = bucky.nextFloat();
        System.out.println("Please enter variables for y1:");
        float y1 = bucky.nextFloat();
        System.out.println("Please enter variables for y2:");
        float y2 = bucky.nextFloat();
        System.out.println("Please enter variables for y3:");
        float y3 = bucky.nextFloat();



        // Formula to calculate centroid 
        float x = (x1 + x2 + x3) / 3;
        float y = (y1 + y2 + y3) / 3;

        System.out.println("Centroid = "
                + "(" + x + ", " + y + ")");
    }
} 
票数 1
EN

Stack Overflow用户

发布于 2021-02-16 05:37:48

您正在尝试除以字符串的和解决方案是使变量( x1,x2,x3等)浮动;将:“strings.The x1 = bucky.nextLine();”改为:"float x1= bucky.nextFloat();“。

代码语言:javascript
复制
public class Centroid{

public static void main(String[] args) {
// coordinate of the vertices 
Scanner bucky = new Scanner(System.in);
System.out.println("Please enter variables for x1:");
float x1 = bucky.nextFloat();
System.out.println("Please enter variables for x2:");
float x2 = bucky.nextFloat();
System.out.println("Please enter variables for x3:");
float x3 = bucky.nextFloat();
System.out.println("Please enter variables for y1:");
float y1 = bucky.nextFloat();
System.out.println("Please enter variables for y2:");
float y2 = bucky.nextFloat();
System.out.println("Please enter variables for y3:");
float y3 = bucky.nextFloat();



// Formula to calculate centroid 
float x = (x1 + x2 + x3)/ 3; 
float y = (y1 + y2 + y3)/ 3; 

System.out.println("Centroid = "
+ "(" + x + ", " + y + ")"); 
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66215182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档