将运算符添加到字符串返回值

时间:2018-05-14 06:49:19

标签: java android

public static String convertTimeintoHrs(String hr, String mn){

        return hr + mn/60;
    }

mn / 60给出错误 - 运算符“/”不能应用于“java.lang.String”,“int”。
如何实现上述而不是解析?

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

试试这个,你必须解析public partial class MainWindow : Window { public int globalServo = 21; public MainWindow() { InitializeComponent(); List<MyData> lstMyData = new List<MyData>(); for (int i = 1; i < 6; i++) { List<MySubData> lstMySubData = new List<MySubData>(); for (int j = 0; j < globalServo; j++) { lstMySubData.Add(new MySubData() { LED = "LED" + j, Servo = "Servo" + j }); } lstMyData.Add(new MyData() { id = i, name = "name-" + i, selected = Convert.ToBoolean(i % 2), lstSubData = lstMySubData }); } lvData.View = GenerateGridView(); lvData.ItemsSource = GenerateSource(lstMyData).DefaultView; } private GridView GenerateGridView() { GridView view = new GridView(); view.Columns.Add(new GridViewColumn() { Header = "Id", DisplayMemberBinding = new Binding("Id") }); view.Columns.Add(new GridViewColumn() { Header = "Name", DisplayMemberBinding = new Binding("Name") }); view.Columns.Add(new GridViewColumn() { Header = "Selected", CellTemplate = GetCheckboxTemplate() }); for (int i = 1; i <= globalServo; i++) { view.Columns.Add(new GridViewColumn() { Header = "Servo" + i, CellTemplate = GetTextBlockTemplate(i) }); } return view; } private DataTable GenerateSource(List<MyData> dataList) { DataTable dt = new DataTable(); dt.Columns.Add("Id"); dt.Columns.Add("Name"); dt.Columns.Add("Selected"); for (int i = 1; i <= globalServo; i++) { dt.Columns.Add("Servo" + i); } foreach (var item in dataList) { DataRow row = dt.NewRow(); row["Id"] = item.id; row["Name"] = item.name; row["Selected"] = item.selected; for (int i = 1; i <= globalServo; i++) { row["Servo" + i] = item.lstSubData[i - 1].Servo + "##" + item.lstSubData[i - 1].LED; } dt.Rows.Add(row); } return dt; } private DataTemplate GetCheckboxTemplate() { DataTemplate dt = new DataTemplate(typeof(CheckBox)); FrameworkElementFactory chkElement = new FrameworkElementFactory(typeof(CheckBox)); dt.VisualTree = chkElement; Binding bind = new Binding(); bind.Path = new PropertyPath("Selected"); chkElement.SetBinding(CheckBox.IsCheckedProperty, bind); return dt; } private DataTemplate GetTextBlockTemplate(int Index) { TextConverter textConverter = new TextConverter(); ColorConverter colorConverter = new ColorConverter(); DataTemplate dt = new DataTemplate(typeof(TextBlock)); FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBlock)); dt.VisualTree = txtElement; Binding bind = new Binding(); bind.Path = new PropertyPath("Servo" + Index); bind.Converter = textConverter; txtElement.SetBinding(TextBlock.TextProperty, bind); Binding bind1 = new Binding(); bind1.Path = new PropertyPath("Servo" + Index); bind1.Converter = colorConverter; txtElement.SetBinding(TextBlock.BackgroundProperty, bind1); return dt; } } String,因为Integer java运算符,并且它需要一个有效的操作数来执行操作

/

深度请阅读文章https://www.tutorialspoint.com/java/number_parseint.htm

答案 1 :(得分:0)

 public static String convertTimeintoHrs(String hr, String mn){
    if(hr !=null && mn !=null) {
        return String.valueOf(Integer.parseInt(hr) + Integer.parseInt(mn) /60);
    }else{
        return null;
    }
}

尝试这个,让我们知道它的工作是否

答案 2 :(得分:0)

使用此:

  public static String convertTimeintoHrs(String hr, String mn){
      Float hours = Float.parseFloat(hr) + Float.parseFloat(mn)/60;
      return String.valueOf(hours);
  }